Open In App

Difference Between Call by Value and Call by Reference in C

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways are generally differentiated by the type of values passed to them as parameters.

The parameters passed to the function are called actual parameters whereas the parameters received by the function are called formal parameters.

Call By Value in C

In call by value method of parameter passing, the values of actual parameters are copied to the function’s formal parameters.

  • There are two copies of parameters stored in different memory locations.
  • One is the original copy and the other is the function copy.
  • Any changes made inside functions are not reflected in the actual parameters of the caller.

Example of Call by Value

The following example demonstrates the call-by-value method of parameter passing

C




// C program to illustrate call by value
#include <stdio.h>
 
// Function Prototype
void swapx(int x, int y);
 
// Main function
int main()
{
    int a = 10, b = 20;
 
    // Pass by Values
    swapx(a, b); // Actual Parameters
 
    printf("In the Caller:\na = %d b = %d\n", a, b);
 
    return 0;
}
 
// Swap functions that swaps
// two values
void swapx(int x, int y) // Formal Parameters
{
    int t;
 
    t = x;
    x = y;
    y = t;
 
    printf("Inside Function:\nx = %d y = %d\n", x, y);
}


Output

Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20

Thus actual values of a and b remain unchanged even after exchanging the values of x and y in the function.

Call by Reference in C

In call by reference method of parameter passing, the address of the actual parameters is passed to the function as the formal parameters. In C, we use pointers to achieve call-by-reference.

  • Both the actual and formal parameters refer to the same locations.
  • Any changes made inside the function are actually reflected in the actual parameters of the caller.

Example of Call by Reference

The following C program is an example of a call-by-reference method.

C




// C program to illustrate Call by Reference
#include <stdio.h>
 
// Function Prototype
void swapx(int*, int*);
 
// Main function
int main()
{
    int a = 10, b = 20;
 
    // Pass reference
    swapx(&a, &b); // Actual Parameters
 
    printf("Inside the Caller:\na = %d b = %d\n", a, b);
 
    return 0;
}
 
// Function to swap two variables
// by references
void swapx(int* x, int* y) // Formal Parameters
{
    int t;
 
    t = *x;
    *x = *y;
    *y = t;
 
    printf("Inside the Function:\nx = %d y = %d\n", *x, *y);
}


Output

Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10

Thus actual values of a and b get changed after exchanging values of x and y.

Difference between the Call by Value and Call by Reference in C

The following table lists the differences between the call-by-value and call-by-reference methods of parameter passing.

Call By Value

Call By Reference

While calling a function, we pass the values of variables to it. Such functions are known as “Call By Values”. While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References.
In this method, the value of each variable in the calling function is copied into corresponding dummy variables of the called function. In this method, the address of actual variables in the calling function is copied into the dummy variables of the called function.
With this method, the changes made to the dummy variables in the called function have no effect on the values of actual variables in the calling function. With this method, using addresses we would have access to the actual variables and hence we would be able to manipulate them.
In call-by-values, we cannot alter the values of actual variables through function calls. In call by reference, we can alter the values of variables through function calls.
Values of variables are passed by the Simple technique. Pointer variables are necessary to define to store the address values of variables.
This method is preferred when we have to pass some small values that should not change. This method is preferred when we have to pass a large amount of data to the function.
Call by value is considered safer as original data is preserved Call by reference is risky as it allows direct modification in original data

Note In C, we use pointers to achieve call-by-reference. In C++, we can either use pointers or references for pass-by-reference. In Java,  primitive types are passed as values and non-primitive types are always references.

Conclusion

In conclusion, Call by Value means passing values as copies to the function, so that the original data is preserved and any changes made inside the function are not reflected in the original data, whereas Call by Reference means passing references to the memory locations of variables (in C we pass pointers to achieve call by reference), hence changes made inside the function are directly modified in the original values. The choice between the two completely depends on the particular requirements and considerations of a program.



Last Updated : 09 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads