#4-Pass by reference and Return by reference in C++-Differences with program source code

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.

Its output will appear like this, as shown below-:

c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy, inline function in c++ with example pdf, advantages and disadvantages of inline function in c++

So, actually what is going inside the program? It is basically a swapping program where

  1. initially x=1(say) y=2(say)
  2. 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

t=a;

a=b;

b=t;

c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy, inline function in c++ with example pdf, advantages and disadvantages of inline function in c++

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.

Syntax-:

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.

 

The output of the above program will be shown as below in Codeblocks.

c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy, inline function in c++ with example pdf, advantages and disadvantages of inline function in c++

So, let’s analyze the program, what’s actually going inside the program.

before

a=5

b=9

after

a=5

b=-1

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.

c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy, inline function in c++ with example pdf, advantages and disadvantages of inline function in c++

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top