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; }
Values inside function call:
x = 5
y = 40
Values after function call:
x = 10
y = 20
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)
//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; }
Values inside function call:
x = 50
y = 40
Values after function call:
x = 50
y = 40
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; } |
No comments:
Post a Comment