In this article call by reference in c When we pass the address of a variable to that function while calling a function it is called call by reference or call by address.
Call by Value and Call by Reference
Call By Value in C:
As we know, no function comes into the program without calling and is not execute. If we need to use any type of function in our program, then first we have to call it.
If we need to call function then first, we must to writing the name of the function then it is called function calling.
While calling the function, if any argument or value is to be passed to that function then we pass that argument or value by writing it in parenthesis () while calling the function.
In c language we can pass two types of values in parenthesis of function then two types of calling function such as follows:
- Call by Value
- Call by Reference in C
Before telling about call by value and call by reference, I want to tell you what difference between the actual parameters and formal parameters are.
While calling a function when we write some value or argument in its parenthesis it is called actual parameters.
While defining the function, when we write or define some argument in parenthesis it is called formal parameters or arguments.
Call By Value in C:
We need to call the function then you pass a value or the value of a variable by writing in it the parenthesis of the function then it is called call by value.
In this the value of the actual parameters is passed or assigned to the formal parameters so that the function can use the value to complete its task.
There is no effect in the original value in call by value because both the values are stored in different locations.
// Perimeter of rectangle
int perimeters (int l, int b); // function declaration
void main ()
int n1=5 , n2=10;
int n3=perimeter (n1, n2); // function call
printf(“perimeter of rectangle is:%d”,n3);
int perimeter (int l, int b) // function definition
O/P:- perimeter of rectangle is : 30.
Advantages of call by value in C:
- It does not change the value of the original variable, which keeps the data secure
- When the function is called it has no effect in the arguments of the actual parameter
- In this the value of the actual parameter goes to the formal parameter, so that if any change is made then its effect is only on the formal parameter, it does not create any problem on the actual parameter.
Disadvantages of call by value in C:
- There is no permission to you cannot change the value of the original variable inside the function
- The actual argument all ways be a variable
- Same value is stored in two different variable which is not memory efficient
Use of call by value:
- When we do not want to change the original value of the variable then we use call by value.
- We use it when we do not want any kind of side effect in the value of the actual parameter.
- When we have to pass only value to the function to do it’s work then we use call by value.
Call by reference in C:
When we pass the address of a variable to that function while calling a function it is called call by reference or call by address.
In this the address of any variable in the actual parameter is passed to the pointer variable (which can store the address of any variable) kept in the formal parameters. By doing this the function can also change the value kept in that address changes also happen in the original value in call by reference because both the values are stored in the same location. In this, the address of the variable kept in the actual parameters is stored in the pointer variable kept in the formal parameters.
// swapping the values of the two variables
void swap (int *p, int *q); // function declaration
void main ()
printf(“Before swap value of n1%d\n”,n1);
printf(“Before swap value of n2%d\n”,n2);
swap (&n1,&n2); // function call
printf(“After swap value of n1%d\n”,n1);
printf(“After swap value of n2%d\n”,n2);
void swap (int*p, int*q) // function definition
Before swap value of x:50
Before swap value of y:60
After swap value of x:60
After swap value of y:50
Advantages of call by reference in C:
- It does not have to create duplicate variable to store the same value.
- It does not cause memory loss.
- In this we can change the value of the original variable inside the function.
- By passing the address we are able to change the value of that variable.
Disadvantages of call by reference in C:
- Variable changes inside the function affect the original variable.
- Call by reference can sometimes cause problems in the program and the program can be complex.
Use of call by reference:
- When we want to make any change in the original value of the variable then we use call by reference.
- We want to change the value of the actual parameter then we use it.
- When we have to pass a reference to that variable for the function to do it’s work then we use call by reference.
Difference between call by value and call by reference.
Call by value
Call by reference