In today’s post, we will be discussing what is call by value vs call by reference? More specifically, we will get to know what is call by value vs call by reference in C++? So, let’s get started!
Well, Call by value and call by reference in cpp are two ways in which a function can be called or invoked.
You must want to know what is cpp?
Like python codes are stored with a .py extension, C++ source codes are stored with a .cpp extension.
Do you want to know what is Call by Value vs Call by reference in C++? Let’s study each of them more carefully.
What is Call by Value in cpp?
The first way to invoke or call a function in C++ is known as ‘call by value.’
In this method, copies of the original values are made and the function works on those copies. Hence, the original values are not changed or modified.
In other words, values of the actual parameters are copied into formal parameters, and the function works on them.
The benefit of using call by value for calling a function in C++ is that the original argument value remains unchanged and intact and hence can be used as per requirements.
Example of Call by Value in C++
Below is an example program for Call by Value.
In the following example, variables a and b are initially assigned values 10 and 20. These variables are called by value and their copies are passed as arguments to the function make_change()
The function then makes changes to those copies and then the results are displayed.
After coming out of the function, the values are again displayed and we see that the changes are not reflected in the original values
#include <iostream> #include <conio.h> using namespace std; void make_change(int a, int b){ a = a + 10; b = b + 5; cout<<"New Values in the function \n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; } int main() { int a, b; a = 10; b = 20; cout<<"\t\t\t CALL BY VALUE EXAMPLE \n"; cout<<"Initial Values \n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; //calling the function by value make_change(a,b); cout<<"Printing the values again to check if changes are applied...\n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; getch(); return 0; }
Output:
In the output of the above example, we see that the values of a and b only get altered when the function make_change() is executed.
After coming out of the function, the final values of the variables are same as the initial values.
This happens because the function make_change() works on the copies of the two variables rather than the original values.
What is Call by Reference in cpp?
Another method to invoke a function is known as ‘call by reference.’ Unlike, call by value, in this method, no copies are made and the function works on the original values.
In call by reference, ‘&’ operation is used and the address of the original values is passed as the arguments to the function.
Hence, the changes which are made by the function are reflected in the original values.
Example of Call by Reference in C++
Here is an example program for Call by Reference.
In the given program, a and b are the two variables with some initial values assigned to them. The addresses of these variables are then passed to the make_change() function with the help of the ‘&’ operator.
The changes that the function makes in the values are reflected in the original values.
We can see the changes even when we come out of the function.
#include <iostream> #include <conio.h> using namespace std; void make_change(int &a, int &b){ a = a + 10; b = b + 5; cout<<"New Values in the function \n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; } int main() { int a, b; a = 10; b = 20; cout<<"\t\t\t CALL BY REFERENCE EXAMPLE \n"; cout<<"Initial Values \n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; //calling the function by reference make_change(a,b); cout<<"Printing the values again to check if changes are applied...\n"; cout<<"a = "<<a<<" b = "<<b<<"\n"; getch(); return 0; }
Output:
Unlike Call by Value, here, we see that the final values of the variables a and b are changed even after coming out from the function make_change().
The reason for this is, we have passed the address of the two variables to the function. Hence, any change the function make_change() makes in the values of a and b gets reflected in the original values.
Summary
Call by value vs Call by reference are the two methods in which a function can be called in cpp.
Both call by value and call by reference has their advantages and can be used as per the requirements of the program.
In call by value, different memory locations are used for both actual and formal arguments. Whereas, in call by reference, the same memory locations are used for the actual and formal arguments.
I hope that this post made it easier for you to understand what is call by value vs call by reference in C++.
So, why wait here? Make your C++ programs using call by value vs call by reference!
If you think you are stuck somewhere and need some help, don’t worry! We are here. Visit https://codingdeputy.com/ today!
Also, check out our other amazing services as well: