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:
- Passes an argument by value.
- Callee does not have any access to the underlying element in the calling code.
- A copy of the data is sent to the callee.
- 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 countint result=0;// initialze the variable resultincrementCount(count);//call increment functioncout<<"Pass by value\n";cout<<"Count:";cout<<count;//prints the value of count after the function callreturn 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:
- Passes an argument by reference.
- Callee gives a direct reference to the programming element in the calling code.
- The memory address of the stored data is passed.
- 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 countint result=0;// initialize the variable resultincrementCount(count);//increment value of countcout<<"Pass by Reference\n";cout<<"Count:";cout<<count;//prints count after the function callreturn 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.
Free Resources