Call by Value and Call by Reference in C

Let’s have a glance at two methods to pass the data into the function in C language, i.e., call by value and call by reference.

The prime difference between call by value and call by reference is that in the call by value a copy of actual arguments is passed to respective formal arguments.

In C language, all function arguments are passed “by value” because C does not support references like other programming languages such as C++ and Java do.

Call by value in C

  • The value of the parameters is copied into the formal parameters in the call by value process. In other words, in the call function call, we can assume that the value of the variable is used by the value method.
    When calling by value process, the value of the actual parameter can not be modified by the formal parameter.
    Due to the value of the actual parameter copied to the formal parameter, separate memory is allocated in call by value.
    The real parameter is the argument used in the function call, while the formal parameter is the argument used in the description of the function.

Let’s try to understand the concept of call by value in c language by the example given below:

Call by Value Example: Swapping the values of the two variables

#include <stdio.h>  
void swap(int , int); //prototype of the function   

int main()  

{  

    int a = 10;  

    int b = 20;   

    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  

    swap(a,b);  

    printf("After swapping values 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 swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10   

}

Output

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20  

Call by reference in C

  • When the variable is called by reference, the address is transferred to the function call as the actual parameter.
    By modifying the formal parameters the value of actual parameters can be updated since the address of the actual parameters has been transferred.
    In call-by-call, the allocation of the memory is identical for both formal and real parameters. All function operations are performed at the value stored at the address of the actual parameters, and the value changed is stored at the same address.

Consider the following example for the call by reference.

#include<stdio.h>  

void change(int *num) {    

    printf("Before adding value inside function num=%d \n",*num);    

    (*num) += 100;    

    printf("After adding value inside function num=%d \n", *num);    

}      

int main() {    

    int x=100;    

    printf("Before function call x=%d \n", x);    

    change(&x);//passing reference in function    

    printf("After function call x=%d \n", x);    

return 0;  

}

Output

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

Call by reference Example: Swapping the values of the two variables

#include <stdio.h>  
void swap(int *, int *); //prototype of the function   

int main()  

{  

    int a = 10;  

    int b = 20;   

    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  

    swap(&a,&b);  

    printf("After swapping values 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 swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10   

}

Output

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10  

Difference between call by value and call by reference in c

No. Call by value Call by reference
1 A copy of the value is passed into the function An address of value is passed into the function
2 Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters.
3 Actual and formal arguments are created at a different memory location Actual and formal arguments are created at the same memory location