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

In C++ programming, there are two ways of passing value to the function: Call by Value or Call by Reference


Before going further into this topic, I will suggest you understand the difference between actual parameters and formal parameters. The actual parameters are those parameters that are passed to the function and formal parameters are those parameters that are received by the function.


Call by Value: Here the values of actual parameters will be copied to formal parameters and these two different parameters store values in different memory locations. Any changes made to the variable inside function are not going to reflect back to the actual parameter. 

Example:

//C++ Example of call by value
#include<iostream>
using namespace std;

//function definition
void update(int x, int y){

   x = 5;
   y = 40;
   cout<<"Values inside function call:"<<endl;
   cout<<"x = "<<x<<endl;
   cout<<"y = "<<y<<endl;
}
int main(){

    int x = 10, y = 20;    
    
    //function call
    update(x, y);
    cout<<"Values after function call:"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl; 

    return 0;
}
Output:
Values inside function call:
x = 5
y = 40
Values after function call:
x = 10
y = 20

call by value example

Here in the above code, we have updated the value of x and y inside the function but that is not affecting the value of our original variable x and y inside the main() function. It is happening because variables inside update() are local to the function and get destroyed later. 


Call by Reference: Here both actual and formal parameters refer to the same memory location. Therefore, any changes made to the formal parameters will get reflected in the actual parameters. 
In Call by Reference, instead of passing values to the function we are passing the memory addresses at which those values are stored using the address of operator (&). The parameters of function are pointers that is used to hold the address of another variable(alert-success)
Example:
//C++ Example of call by reference
#include<iostream>
using namespace std;

//function definition
void update(int *x, int *y){

   *x = 50;
   *y = 40;
   cout<<"Values inside function call:"<<endl;
   cout<<"x = "<<*x<<endl;
   cout<<"y = "<<*y<<endl;
}
int main(){

    int x = 10, y = 20;    
    
    //function call
    update(&x, &y);
    cout<<"Values after function call:"<<endl;
    cout<<"x = "<<x<<endl;
    cout<<"y = "<<y<<endl; 

    return 0;
}
Output:
Values inside function call:
x = 50
y = 40
Values after function call:
x = 50
y = 40

call by reference
Here in the above code, we are passing the address of the variable &x and &y to the function update() and the parameters of this function are pointers (*x and *y). A pointer is used to hold the address of another variable and you can fetch the value present at that particular address using dereference operator (*).  

Difference Between Call by Value and Call by Reference.

Call By Value Call By Reference
Copies of arguments are passed to the function. Changes to parameters inside the function do not affect the original values. References to the original arguments are passed to the function. Changes to parameters inside the function directly affect the original values.
Syntax: void functionName(int a, int b) Syntax: void functionName(int &a, int &b)
Changes inside the function do not affect the original variables. Changes inside the function directly affect the original variables.
May have a performance impact if large data structures are passed since copies are made. Typically more efficient as no copies are made, especially for large data structures.
// Call By Value Example
void swap(int x, int y) {
int temp = x; x = y;
y = temp;
}
int main() {
int a = 5, b = 10;
swap(a, b); cout << "a: " << a << ", b: " << b;
return 0;
}
// Call By Reference Example
void swap(int &x, int &y) {
int temp = x; x = y; y = temp;
}
int main() {
int a = 5, b = 10; swap(a, b); cout << "a: " << a << ", b: " << b;
return 0;
}


Read more:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS