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.