Actual parameters: The parameters that appear in function calls. Formal parameters: The parameters that appear in function declarations. Pass the actual parameters while calling a function Values of actual parameters are copied to the formal parameters
When we pass the actual parameters while calling a function then this is known as function call by value. In this case the values of actual parameters are copied to the formal parameters. Thus operations performed on the formal parameters don’t reflect in the actual parameters.
Example 1 : Simple increment of integer
#include <stdio.h>
int increment(int var)
{
var = var+1;
return var;
}
int main()
{
int num1=20;
int num2 = increment(num1);
printf("num1 value is: %d", num1);
printf("\nnum2 value is: %d", num2);
return 0;
}
Output
num1 value is: 20
num2 value is: 21
We passed the variable num1 while calling the method, but since we are calling the function using call by value method, only the value of num1 is copied to the formal parameter var. Thus change made to the var doesn’t reflect in the num1.
Example 2: Swapping numbers using Function Call by Value
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
/*Copying var1 value into temporary variable */
tempnum = var1 ;
/* Copying var2 value into var1*/
var1 = var2 ;
/*Copying temporary variable value into var2 */
var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);
/*calling swap function*/
swapnum(num1, num2);
printf("\nAfter swapping: %d, %d", num1, num2);
}
Output
Before swapping: 35, 45
After swapping: 35, 45