Home >>C Tutorial >C call By Value and Reference

Difference between Call by Value and Call by Reference

The Call by value and call by reference in the C language

In order to pass the data into a function in the C language there are two methods that are call by value and call by reference.

In this tutorial we will be discussing these two methods in brief in order to understand them:

Call by value method in the C language

  • The call by value method in C language generally copies the values of actual parameters in to the formal parameters. In simpler words, the value of the variable is used in the function call in the call by value method.
  • In the call by value method, the value of the actual parameter cannot be modified by the formal parameter.
  • In call by value, as value of the actual parameter is copied into the formal parameter hence, different memory is allocated.
  • The argument that is used in the function call is the actual parameter whereas formal parameter is the argument that is used in the function definition.

Here is an example of the call by value in the C language to clear the basic concept:

#include<stdio.h>  
void change(int num) 
{    
    printf("Beforehand the value addition inside function num=%d \n",num);    
    num=num+100;    
    printf("After the value is adeded inside function num=%d \n", num);    
}    
int main() 
{    
    int x=100;    
    printf("Beforehand the function call x=%d \n", x);    
    change(x);//passing value in function    
    printf("After the function call x=%d \n", x);    
	return 0;  
}    
Output :
Beforehand the function call x=100
Beforehand the value addition inside function num=100
After the value is added on inside function num=200
After the function call x=100

Here is another example(Swap value of two variables)

#include <stdio.h>  
void swap(int , int);    
int main()  
{  
    int a = 10;  
    int b = 20;   
    printf("Beforehand the values are swapped in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  
    swap(a,b);  
    printf("Afterhand the values are swapped in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20  
}  
void swap (int a, int b)  
{  
    int temp;   
    temp = a;  
    a=b;  
    b=temp;  
    printf("After the values are swapped in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10   
} 
Output :
Beforehand the values are swapped in main a = 10, b = 20
After the values are swapped in function a = 20, b = 10
Afterhand the values are swapped in main a = 10, b = 20

Call by reference in the C language

  • In call by reference method, variable's address is passed into the function call as the actual parameter.
  • The value of the actual parameter's value can be modified just by changing the formal parameters as the address of the actual parameters is already passed.
  • In the call by reference method in C, the memory allocation has very similarities with both the formal parameters and the actual parameters. The entire operations in the function are basically performed on the value stored at the address of the actual parameters. The modified values are stored at the same address.

Here is an example of call by reference:

#include<stdio.h>  
void change(int *num) 
{    
    printf("Before the values are added inside function num=%d \n",*num);    
    (*num) += 100;    
    printf("After the values are added inside function num=%d \n", *num);    
}      
int main() {    
    int x=100;    
    printf("Before the function call x=%d \n", x);    
    change(&x);//passing reference in function    
    printf("After the function call x=%d \n", x);    
return 0;  
}
Output :
Before the function call x=100
Before the value is added inside function num=100
After the values are added inside function num=200
After the function call x=200

Here is another example of the call by reference method in C:

#include <stdio.h>  
void swap(int *, int *); //prototype of the function   
int main()  
{  
    int a = 10;  
    int b = 20;   
    printf("Before the values are swapped in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  
    swap(&a,&b);  
    printf("After the values are swapped in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20  
}  
void swap (int *a, int *b)  
{  
    int temp;   
    temp = *a;  
    *a=*b;  
    *b=temp;  
    printf("After the values are swapped in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10   
}  
Output :
Before the values are swapped in main a = 10, b = 20
After the values are swapped in function a = 20, b = 10
After the values are swapped in main a = 20, b = 10

The difference between call by value and call by reference in C language:

The C language is known to for its feasibility and functions. Now the functions needed to be called in order to execute them and they can be invoked in these two ways i.e. Call by Value or Call by Reference in C language. These are the two ways that are generally known to be differentiated by the type of values passed by them as their parameters. There’s a catch in understanding parameters, actually the parameters that are passed to function are called actual parameters and the parameters that are received by function are called formal parameters.

Call by value Call by reference
In this method, a copy of the value is passed into the function. In this method, an address of the value is passed into the function
The changes that are made inside the function is only limited to the function. By changing the formal parameters the values of the actual parameters does not change. The changes that are made inside the function also validate outside of the function. By changing the formal parameters the values of the actual parameters does change.
The memory location is different where the actual and formal arguments are created. The memory location is the same where the actual and formal arguments are created.

No Sidebar ads