Functions in c
A function in C is a block of code that performs a specific task. It can take inputs, process them, and produce outputs. Functions provide a way to organize code into manageable pieces, promote reusability, and enhance the readability of programs.
A function in C follows a basic syntax :
return_type function_name(parameters) {
  // function body
}
return_type : The type of value the function returns to the calling code. It can be 'void' if the function doesn't return anything.
function_name : The name of the function, which is used to call it from other parts of the program.
parameters : Any values passed to the function for processing. These are optional.
function body : The block of code that defines what the function does.
To use a function, you simply call it by its name, passing any required arguments inside parentheses. For example : Calling the add function with arguments 3 and 4
int result = add(3, 4);
Functions may return a value using the 'return' statement. It exits the function and passes a value (if required) back to the caller.
return a + b;
Functions that don't return any value are termed as 'void' functions. They are declared and defined with void as the return type.
void sayHello() {
  printf("Hello, PBA INSTITUTE!");
}
Output :
Add=7
In this example :
- 'add' is the name of the function.
- 'int' before the function name specifies the return type of the function. In this case, the function returns an integer.
- 'int a' and 'int b' are the parameters passed to the function.
- Inside the function body, 'a' and 'b are added together, and the result is returned using the return statement.
- In the main function, 'add(3, 4)'' is called, and the result is stored in the result variable.
- Finally, the 'printf' function is used to print the sum.
Output :
Enter a number=4
Even
Output :
sum=55
Output :
enter a number=123
last digit is=3
Functions are fundamental building blocks in C programming, offering modularity, reusability, abstraction, maintainability, scalability, readability, performance optimization, encapsulation, and information hiding. Mastering the use of functions is essential for writing efficient, robust, and maintainable C programs.