Open In App

Difference Between Call by Value and Call by Reference in C++

Last Updated : 15 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++ programming we have different ways to pass arguments to functions mainly by Call by Value and Call by Reference method. These two methods differ by the types of values passed through them as parameters.

Before we look into the call-by-value and call-by-reference methods, we first need to know what are actual and formal parameters.

The actual parameters also known as arguments are the parameters that are passed into the function when we make a function call whereas formal parameters are the parameters that we see in the function or method definition i.e. the parameters received by the function.

Call by Value in C++

In the call-by-value method, function arguments are passed by copying the value of the actual parameter, ensuring the original values remain unchanged. The value is copied to the formal parameter.

One is the original copy and the other is the function copy. Any changes made to the parameters within the function do not change the original values outside the function.

Example of Call by Value

The below example demonstrates the working of call by value by updating the original value of a variable.

C++




// C++ program to demonstrate call by value
  
#include <iostream>
using namespace std;
  
// function to update the original value
void increment(int num)
{
    num++;
    cout << num << endl;
}
  
int main()
{
    int number = 5;
    increment(number); // Passing 'number' by value
    cout << number << endl;
    return 0;
}


Output

6
5

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 the call-by-reference method, the memory address (reference) of the actual parameter is passed to the function, allowing direct access and modification of the original values. The actual and the formal parameters point to the same memory address. Any changes made to the parameters within the function are directly reflected in the original values outside the function.

Example of Call by Reference

The below example demonstrates the working of call by reference.

C++




// C++ program to demonstrate the working of call by
// reference
  
#include <iostream>
using namespace std;
  
// function to update the original value
void increment(int& num)
{
    num++;
    cout << num << endl;
}
  
int main()
{
    int number = 5;
    increment(number); // Passing 'number' by reference
    cout << number << endl;
    return 0;
}


Output

6
6

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”.

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

The major difference between the two methods of passing the parameters is given below:

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 memory address of the variable is passed.
Scope of Changes In this method, the original value remains unchanged even when we make changes in the function. In this method, the changes made are reflected in the original variable.
Performance It may require extra memory and time to copy so less efficient. It is more memory and time efficient as compared to “call by value”.
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.

Conclusion

By understanding the difference between the two methods of passing parameters that is “call by value” and “call by reference”, we can choose the appropriate parameter passing method based on the specific requirements and functionality needed in a C++ program. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads