Large programs in C can be divided into basic small blocks called functions. A function is a set of statement that performs a task. It takes an input, does some computation and gives an output. It can also be called multiple times in the program. Every C program has at least one function, which is main() and the other functions come inside it.
The general syntax of a function is:
return_type function_name([ arg1_type arg1_name, ... ]) { body of function} |
where, return_type is the data type of the value that the function returns. It may return a value or be void. function_name is the actual name that is given to the function.
Argument: A value is passed to the argument when a function is invoked. This is actual parameter or argument.
Advantage of functions in C
- We can avoid using same logic or code multiple times in a program by using functions.
- Reusability: We can call the functions any number of times and from any place in a C program.
- Dividing the program into multiple functions helps to track large C programs.
- However, Function calling is always a overhead in a C program.
Function Declarations
In C, a function declaration is global and tells the compiler about a function name and how to call the function.
return_type function_name( parameter list );
Example:
int max(int num1, int num2);
Calling a Function
In a C function, we give a definition of what the function has to do. So, to use this function in the program, we call the function to perform the defined task. It can also be called multiple times in a program.
When we call a function, we transfer the program control to the called function. The called function performs the task defined. Then, after the return statement is executed it returns the program control back to the main program.
Example: Program to get max value using functions
#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 150; int b = 250; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }
Output:
Max value is : 250
Function Arguments
The function must declare variables that accept the values of the arguments to use them. These variables are also called the formal parameters of the function.
The formal parameters are like other local variables inside the function. Hence, they are created upon entry into the function and destroyed upon exit.
There are two ways in which arguments can be passed to a function:
Sr.No. | Call Type & Description |
---|---|
1. | Call by value: The actual value of an argument is copied into the formal parameter of the function. Hence, the changes made to the parameter inside the function has no effect on the actual parameter or argument. |
2. | Call by reference: The address of an argument is copied into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. Hence, changes made to the parameter affect the actual parameter/argument. |
Example 1: Call by value
#include <stdio.h> void fun(intx){ x = 50; }
int main(void) { int x = 30; fun(x); printf("x = %d", x); return0; }
Output:
x = 30
Example 2: Call by Reference
# include <stdio.h> void fun(int*ptr) { *ptr = 50; }
int main() { int x = 30; fun(&x); printf("x = %d", x); return 0; }
Output:
x = 50
Both the actual and formal parameters refer to same locations. By default, C language uses the call by value method to pass arguments.