Function Call in C

A function call is an  important part of the C programming language. It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function.
Syntax :

#include <stdio.h>  
int add(int a, int b);   
void main()  
{  
  
int sum;  
int a, b;  
printf(" Enter the first and second number \n");  
scanf("%d %d", &a, &b);  
sum = add(a, b); // call add() function  
printf( "The sum of the two number is %d", sum);  
}  
int add(int n1, int n2) // pass n1 and n2 parameter  
{  
int c;  
c = n1 + n2;  
return c;  
}  
Output :
Enter the first and second number
5
6
The sum of the two number is 11

Call by value

When single or multiple values of an actual argument are copied into the formal parameter of a function, the method is called the Call by Value. Hence, it does not alter the function's actual parameter using the formal parameter.

Consider a program to demonstrate the Call by Value in C programming.
#include <stdio.h>  
int main()  
{  
int x = 10, y = 20;  
printf (" x = %d, y = %d from main before calling the function", x, y);  
CallValue(x, y);  
printf( "\n x = %d, y = %d from main after calling the function", x, y);  
}  
int CallValue( int x, int y)  
{  
x = x + 5;  
y = y + 5;  
printf (" \nx = %d, y = %d from modular function", x, y);  
}   

 Output :

x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
 x = 10, y = 20 from main after calling the function

Call by reference 

In this method, the address of the actual argument is copied into the function call's formal parameter; the method is known as Call by Reference. If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter.

Consider a program to demonstrate the Call by Reference in C programming.

#include <stdio.h>  
int main()  
{  
int x = 10, y = 20;  
printf (" x = %d, y = %d from main before calling the function", x, y);  
CallValue (&x, &y);  
printf( "\n x = %d, y = %d from main after calling the function", x, y);  
}  
int CallRef( int *a, int *b)  
{  
*a = *a + 5;  
*b = *b + 5;  
printf (" \nx = %d, y = %d from modular function", *a, *b);  
}  

Output :

x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
 x = 15, y = 25 from main after calling the function

Posted on by