returning references from functionsjust as in


Returning References from Functions

Just as in passing the parameters by reference, returning a reference also doesn't return back a copy of the variable , instead an alias is returned.

e.g.

                int &func(int &num)

                 {

                                :

                                :

                                return(num);

                 }

                void main()

                 {

                   int n1,n2;

                                :

                                :

                                n1 = fn( n2);

                 }

Notice that the function header haves an ampersand (&) before the function name. This is how a function is made to return reference variable. In this case, it takes a reference to an integer as its argument and returns a reference to an integer. This facility can be very useful for returning objects and even structure variables.

 

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: returning references from functionsjust as in
Reference No:- TGS0309416

Expected delivery within 24 Hours