Reference Parameters
One importance use for a reference is to permit you to create functions which automatically use call-by-reference parameter that passing rather than c++'s default call- by-value.
As you know, in C, to create a call-by-reference you have to explicitly pass the address of an argument to the function. For instance, let consider the following short program, that uses this approach in a function known as neg(), that reverses the sign of the integer variable pointed to through its argument.
#include<iostream.h>
void neg(int *i);
main()
{
int x;
x=10;
cout << x<<"negated is ";
neg(&x);
cout <<x<<"\n";
return 0;
}
void neg(int *i)
{
i = -i;
}
Within this program, neg() takes as a parameter a pointer to the integer whose sign it will reverse. Thus, neg() is explicitly known as with the address of x. In addition, inside neg() the * operator must be used to access the variable pointed to through i. As you could automate these characteristics through using a reference parameter.
to make a reference parameter, precede the name of parameter with an. Here is how neg() is declared by using a reference:
Void neg(int &i);
This would tell the compiler to make i into a reference parameter. Once this has been complete, i essentially becomes another name for whatever argument neg() is known with. Which is, i is an implicit pointer which automatically refers to the argument used in the call to neg(). At one i has been made into a reference, that is no longer necessary (or even legal) to apply the * operator. Alternate, every time i is used, it is implicitly a reference to the argument's name with the & operator. Alternate, the compiler does this automatically. In the given program is the reference version of the preceding program
#include<iostream.h>
void neg(int &i);
main()
{
int x;
x=10;
cout << x<< "negated is ";
neg(x);
cout << x<<"\n";
return 0;
}
void neg(int &i)
{
i= -i;
}
to review: whenever you create a reference parameter, that particular parameter automatically refers to (implicitly points to) the argument used to call the function. Therefore, the declaration
i=-1;
actually operates on x, not on a copy of x. There is no requirement to apply the & operator to an argument. In addition, within the function, the reference parameter is used straight without the requirement to apply the * operator.
This is most important to understand that when you assign a value to a reference, you are in fact assigning that value to the variable used in the call to the function.
Within the function, it is not possible to modify what the reference parameter is "pointing" to. Which is, a statement such as
i++;
Inside neg() increments the value of the variable used in the call. That does not cause i to point to a few new location.
Here is another instance. This program uses reference parameters to swap the values of the variables it is known as with.
#include<iostream.h> void swap(int &i,int &j); main()
{
int a,b,c,d;
a=1; a=2; a=3; d=4;
cout << "a and b " << a << " " << b << "\n";
swap(a,b);
cout <<"a and b " <<a << " " << b<<"\n";
cout << "c and d" << c<< " " << d << "\n;
swap(c,d);
cout << "c and d:" << c<< " " << d << "\n";
return 0;
}
void swap(int &i,int &j)
{
int t; t=i; i=j; j=t;
}
this program displays the following:
a and b: 1 2
a and b: 2 1
c and d: 3 4
c and d: 4 3