Call by Value and Call by Reference
Call by Value and Call by Reference
There are two methods to pass the data into the function in C language, i.e., call by value
and call by reference.
Call by value in C
● In the call by value method, the value of the actual parameters is copied into the
formal parameters.
● In the call by value method, we can not modify the value of the actual parameter
by the formal parameter.
● In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.
● The actual parameter is the argument which is used in the function call whereas
the formal parameter is the argument which is used in the function definition.
● Call by value method copies the value of an argument into the formal
#include<stdio.h>
int main()
int a = 10;
int b = 20;
int temp;
temp = a; //temp=10
a=b; //a=20
b=temp; //b=10
Output:
--------------------------------
Call by reference in C
● In call by reference, the address of the variable is passed into the function call as
the actual parameter.
● The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
While calling a function, instead of passing the values of variables, we pass address
of variables(location of variables) to the function known as “Call By References.
#include <stdio.h>
// Function Prototype
void swapx(int* x, int* y);
// Main function
int main()
// Pass reference
return 0;
int t;
t = *x; //t=10
*x = *y; //*x=20
*y = t; //*y=10
}
Output:
--------------------------------
Differences:-