Call: Value & Reference in C

In C, when passing arguments to a function, there are two methods: Call by Value and Call by Reference. Understanding these two methods is essential for effective function management in C programming.

Call by Value and Reference in C

Let's understand Call by Value and Call by Reference in C language one by one:

1) Call by Value

In Call by Value, the actual value of the argument is passed to the function. The changes made to the parameter inside the function do not affect the original argument.

Example to demonstrate Call by Value:

Example

                        
#include <stdio.h>  
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:

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

2) Call by Reference

In Call by Reference, the address (reference) of the argument is passed to the function. The function can modify the actual value of the argument since it operates directly on the original variable.

Example to demonstrate Call by Reference:

Example

                        
#include  <stdio.h>
// Function to swap two integers
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);  
}  
        
int main() 
{  
    int a, b;  
        
    // User input
    printf("Enter the value of a: ");  
    scanf("%d", &a);  
    printf("Enter the value of b: ");  
    scanf("%d", &b);  
    
    printf("Before swapping values in main: a = %d, b = %d\n", a, b);  
    
    // Call the swap function
    swap(&a, &b);   
    
    printf("After swapping values in main: a = %d, b = %d\n", a, b);  
   
    return 0;  
}    
      
                    

Output:

Enter the value of a: 5 Enter the value of b: 10 Before swapping values in main: a = 5, b = 10 After swapping values in function: a = 10, b = 5 After swapping values in main: a = 10, b = 5

Difference between Call by Value and Call by Reference

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 the different memory location Actual and formal arguments are created at the same memory location