C++ call by value and call by reference

In this tutorial, you will learn about call by value and call by reference in C++ with the help of examples.


In C++ you can pass values to a function in two ways which are:

  • Call by Value
  • Call by Reference

In Call by value, you pass a copy of the values present in the variables and the function works on those copies of the variables to return an answer. Any changes to the formal parameters do not change the values of the actual parameters.

In Call by reference, you pass the original variables to the function, and any changes made to them in the function get reflected back to the actual parameters and there is only one copy of the variable created which the function works on.

In C++ we can pass normal variables for call by value but to use call by reference we need to pass the address of the variable with the help of pointer manipulation and the function then uses the pointer to access the same memory location and work on those variables.

Call by Value

Here in this example, we can see that the original value does not get modified. The value being passed is locally stored in a memory location. Any changes in the value will only change the local function and not the original value.

// C++ call by Value
#include <iostream> 
using namespace std;  

void funct(int x)   // Only a copy of the variable is passed
{
    x = 30;
}

int main() {
    int x = 10;
    funct(x);   // Call by value where the variable is passed
    cout << "x = " << x;
    return 0;
}

Output

x = 10

Call by Reference

While using call by reference, the value does get changed as we are passing an address. The actual and formal parameters both refer to the same memory location and value. So any value that is changed in the function gets actually changed in the original value.

// C++ call by Reference
#include <iostream>  
using namespace std;  

void funct(int *p) // The address of the variable is accepted by a pointer
{
    *p = 40;   // The value at the address p is updated
}

int main() {
    int x = 10;
    funct(&x);  // Call by Reference where the address is passed
    cout << "x = " << x;
    return 0;
}

Output

x = 40

Key Points to Remember

For C++ programs, you need to remember the following points for Call by Value and Call by Reference:

  • Call by values do not change the values that are passed but Call by reference does.
  • You have a choice to pass a variable either by value or by reference except for arrays and functions.
  • Arrays and functions are always passed and returned as Call by reference and using pointer operations
  • Call by reference is more memory efficient than Call by value.

Difference Between Call by Value and Call by Reference

The key differences between them are:

Call by Value Call by Reference
A copy of the value is passed to the function An address of the value is passed to the function
Any change to the value does not reflect back to the original value All changes get reflected back to the original value
Both Formal and Actual Arguments have different memory locations The same memory location is reserved for the Formal and Actual argument

 

Please get connected & share!

Advertisement