Today we will discuss about Pass by reference and return by reference in C++.
Before moving forward, first forget that & sign means address. It was in C. And now we are discussing C++. The & means reference variable(which will be briefly discussed in theory part of C++).
So, let’s move ahead!
Pass by reference-:
In pass by reference, the formal arguments in the called function becomes aliases to the actual arguments in the calling function. i.e
When we are working with formal arguments actually, we are working in the original data.
using namespace std;
void swap(int &a,int &b)
cout<<"\nbefore swapping, x=\n"<<x<<"\ny=\n"<<y;
Its output will appear like this, as shown below-:
So, actually what is going inside the program? It is basically a swapping program where
- initially x=1(say) y=2(say)
- then finally, we want the datas to be swapped. i.e y=1, x=2.
but we are going to do this with the help of the reference-i.e pass by reference.
Let’s start the program flow-:
So, the main function starts. Then x=5, y=9 (according to the above program) .
before swapping, x=5, and y=9;
then swap function is called where we have passed x, and y. So, these are actual arguments. It is said above that the formal arguments in the called function becomes aliases to the actual arguments in the calling function.i.e they become similar copy of the actual arguments.
Then, the swap function starts to run.
Here x=&a; this is what the above bolded letters want to say above.
then we can see a swapping code right there i,e
Return by reference-:
In c++, a function can return alias=copy/duplicate of the variable in return statement.
so, here is nothing happening like that of above as
formal argument=alias to actual arguments; but; what is happening right here is that, an alias of the variable is going to be returned.
Data-type& function _name(data-type&variable_name)
//body of the function
Here, the return type of function in above syntax is data-type &, which means that function actually returns alias of the variable and not the value.
using namespace std;
int& max(int&x,int &y)
cout<<"\nbefore calling the function,a=\n"<<a<<"\nand b\n"<<b;
cout<<"\nafter calling the function,a=\n"<<a<<"\nb=\n"<<b;
The output of the above program will be shown as below in Codeblocks.
So, let’s analyze the program, what’s actually going inside the program.
Let’s dive into the int main() function.
a is initialized a value 5 whereas b is initialized a value 9.
so, before calling a=5,b=9.
A part of output has been satisfied till now.
Now, max(a,b) is called; obviously, the max of a and b;i.e a=&x, b=&y;(return the alias of the variable i.e the duplicate of the variable)
Then, here b=9 will be returned. but; that value will be hanged and made b=-1 as the function max(a,b) equals to -1.
so, after calling, we get a=5,b=-1.
So, I hope the program is very much clear. If you still have some doubts, then ask on the comment section. I will be happy to help some of you. You can also connect with me on my facebook page Thomas Alva Edison.