Difference Between Call by Value and Call by Reference in C++
Last Updated :
07 Mar, 2025
In C++, there are two primary methods to pass an argument to a function: Call by Value and Call by Reference. These methods dictate how data is transferred to functions and how changes to the data are handled.
Before we look into the difference, we first need to know what actual and formal parameters are. The actual parameters (also known as arguments) are the parameters that are passed in function call whereas formal parameters are the parameters that we see in the function or method definition.
The below table lists the primary differences between the Call by Value and Call by Reference:
Feature | Call by Value | Call by Reference |
---|
Value Passed | In this method, the value of the variable is passed to the function. | In call by reference, reference to the variable is passed. |
Scope of Changes | The original value remains unchanged even when we make changes in the function. | The changes made are reflected in the original variable. |
Performance | It requires extra memory and time to copy so less efficient. | It is more memory and time efficient as it does not create a copy. |
Memory Location | The memory addresses of the actual and formal parameters are different. | The actual and the formal parameters point at the same memory address. |
Applications | Mainly used to pass values for small data or when we do not want to change original values. | It is used when we want to modify the original value or save resources. |
Call by Value in C++
In Call by Value, the actual value of the argument is passed to the function. A copy of the argument is made, and the function works on this copy. The original variable in the calling function remains unaffected by any changes made to the parameter inside the function.
Example:
C++
#include <iostream>
using namespace std;
// Function to update the original value
void increment(int n){
n++;
cout << n << endl;
}
int main()
{
int num = 5;
// Passing 'number' by value
increment(num);
cout << num << endl;
return 0;
}
Explanation: In the above program the "number" is passed as an actual parameter to the function increment and in the function as a formal parameter "num" that stores the value of "number". In the function, the value of "num" is incremented but there is no change in the value of "number". This is what we say "call by value".
Call by Reference in C++
In Call by Reference, instead of passing a copy of the argument, the memory address (reference) of the variable is passed to the function. This allows the function to directly modify the original argument, as both the formal and actual parameters refers to the same variable.
Example:
C++
#include <iostream>
using namespace std;
// function to update the original value
void increment(int& num)
{
num++;
cout << num << endl;
}
int main()
{
int number = 5;
// Passing 'number' by reference
increment(number);
cout << number << endl;
return 0;
}
Explanation: In the above code the variable "number" is passed to function increment. A formal parameter "num" points to the same address as "number". When the function increments the value of the parameter, the changes made in "num" is reflected in "number" as we can see from the output. This is what we say "call by reference".