Call by value and Call by reference in C

In C language we can pass arguments to a function using below two ways.

  • call by value
  • call by reference
call by value and call by reference in c

Call by value in C

  • The value of the actual parameters is copied into the formal parameters. So any operation performed by function on arguments doesn’t affect actual parameters. It only performs the operation on formal arguments.The value of the variable is used in the function call in the call by value method.
  • We can not modify the value of the actual parameter by the formal parameter.
  • 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 formal parameter is the argument which is used in the function definition.

Below is the example of call by value

snippet
#include
void change(int num) {  
    printf("Before adding value inside function num=%d \n",num);  
    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 value in function  
    printf("After function call x=%d \n", x);  
return 0;
}

Output

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

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

snippet
#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

  • The address of the actual arguments (or parameters) is passed to the formal parameters. So any operation performed on formal parameters affects the value of actual parameters.
  • The value of the actual parameters will be modified by changing the formal parameters since the address of the actual parameters is passed.
  • The memory allocation is similar for both formal parameters and actual parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.

Consider the following example for the call by reference.

snippet
#include
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

snippet
#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

call by value vs call by reference in c

No.Call by valueCall by reference
1A copy of the value is passed into the functionAn address of value is passed into the function
2Changes 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.
3Changes made to the parameter are not reflected in the original parameter.Changes made to the parameters are reflected in the original parameter.
4Actual and formal arguments are created at the different memory locationActual and formal arguments are created at the same memory location
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +