Call by reference and Call by value in C++

In this tutorial, we will learn about call by reference and call by value in C++.

In C++, we can call or invoke functions using two ways: call by reference or call by value. Their difference is basically about the type of arguments that are passed to the function. The parameters passed to the function are called actual parameters and the parameters received by the function are called formal parameters. We shall be using these terms in this article for a better understanding of the given concept.

Call by value in C++

In this method, the values of actual arguments are copied into the formal arguments.  Therefore, any changes made to the formal arguments do not reflect in actual arguments. That is, no change occurs in actual arguments and they remain the same. Also, here, the memory allocated to both arguments are different. That is, their values are stored in different memory locations.

C++ Program : Call by Value

#include <iostream>
using namespace std;

void value(int a1, int b1)
{
  a1++; b1++;           //increment in formal arguments
  cout << "\n\n Call by value " << endl;
  cout << " ------------- " << endl;
  cout << " a = " << a1 << endl;     //output of formal arguments
  cout << " b = " << b1 << "\n\n";
}

int main()
{
  int a, b;
  cout << " Enter the value of a,b : ";
  cin >> a >> b;
  value(a, b);

  cout << " back to main " << endl;
  cout << " ------------ " << endl;
  cout << " a = " << a << endl;     //output of actual arguments
  cout << " b = " << b << endl;

  return 0;
}

Output :

Enter the value of a,b :
10
20


Call by value
-------------
a = 11
b = 21

back to main
------------
a = 10
b = 20

Advantage of the call by value

The biggest advantage of the call by value is that the data remains secure. However, we use more memory in this method. It is because the same value is stored in two different places. This makes the code less efficient.

Call by reference in C++

In this method, the address of the actual parameters is passed to the formal parameters. Hence, it is the address that is sent to the function and not a value. Therefore, any changes made to the formal parameters reflect in the actual parameters. That is the value of the actual parameter changes according to the changes made in the function. Also, here, there is no extra memory allocated to the formal argument. That is, their values are stored in the same memory location.

C++ Program: Call by reference

#include <iostream>
using namespace std;

void value(int &a1, int &b1)
{
  a1++; b1++;              //increment in formal arguments
  cout << "\n\n Call by reference " << endl;     
  cout << " ----------------- " << endl;
  cout << " a = " << a1 << endl;        //output of formal arguments
  cout << " b = " << b1 << "\n\n";
}

int main()
{
  int a, b;
  cout << " Enter the value of a,b : ";
  cin >> a >> b;
  value(a, b);

  cout << " back to main " << endl;
  cout << " ------------ " << endl;
  cout << " a = " << a << endl;          //output of actual arguments
  cout << " b = " << b << endl;

  return 0;
}

Output :

Enter the value of a,b :
10
20


Call by reference
-----------------
a = 11
b = 21

back to main
------------
a = 11
b = 21

Advantage of the call by reference

The biggest advantage of the call by reference is that it is memory efficient. Also, because of no duplicate memory used, we can save the memory space. However, as the original data (actual arguments) can be modified, it makes the code a little less secure.

Conclusion

In the end, both methods have their advantages as well as disadvantages. It depends on the program, which is more apt to apply.

Hope this was helpful. Enjoy coding!

Also, learn :

Break and Continue in C++

Pointers and references in C++

Leave a Reply

Your email address will not be published. Required fields are marked *