Pass by value vs. pass by reference

When a function is called, the arguments in a function can be passed by value or passed by reference.

Callee is a function called by another and the caller is a function that calls another function (the callee).

The values that are passed in the function call are called the actual parameters.

The values received by the function (when it is called ) are called the formal parameters.

Pass by value

Pass by value means that a copy of the actual parameter’s value is made in memory, i.e. the caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is not visible to the caller.

Overview:

  1. Passes an argument by value.
  2. Callee does not have any access to the underlying element in the calling code.
  3. A copy of the data is sent to the callee.
  4. Changes made to the passed variable do not affect the actual value.

Coding example

Now let's explore it further with a coding example.

#include<iostream>
using namespace std;
void incrementCount(int count)//pass by value
{
count=count+1;//increments the value of count inside the function
}
int main()
{
int count=0;// initialze the variable count
int result=0;// initialze the variable result
incrementCount(count);//call increment function
cout<<"Pass by value\n";
cout<<"Count:";
cout<<count;//prints the value of count after the function call
return 0;
}

Pass by reference

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter. If the callee modifies the parameter variable, the effect is visible to the caller’s variable.

Overview:

  1. Passes an argument by reference.
  2. Callee gives a direct reference to the programming element in the calling code.
  3. The memory address of the stored data is passed.
  4. Changes to the value have an effect on the original data.

The following code illustrates the concept.

#include<iostream>
using namespace std;
void incrementCount(int & count)//& to pass by reference
{
count=count+1;//increments the value of count
}
int main()
{
int count=0;//initialize the variable count
int result=0;// initialize the variable result
incrementCount(count);//increment value of count
cout<<"Pass by Reference\n";
cout<<"Count:";
cout<<count;//prints count after the function call
return 0;
}

Note: In the examples, we can clearly see that the value of variable "count" is not updated if it is passed by value. However, it gets updated when the variable "count" is passed by reference.

When to use pass by value?

If we are building multi-threaded application, then we don’t have to worry of objects getting modified by other threads. In distributed application pass by value can save the over network overhead to keep the objects in sync.

When to use pass by reference?

In pass by reference, no new copy of the variable is made, so overhead of copying is saved. This makes programs efficient especially when passing objects of large structs or classes.

Copyright ©2024 Educative, Inc. All rights reserved