Example
#include <iostream.h>
int sqr_it(int x); // declares the return data type as an integer
main()
{
int t=10;
cout << sqr_it(t) <<' '<<t;
return 0;
}
sqr_it(int x)
{
x=x*x;
return x;
}
In this example, the value of the argument to sqr_it (), 10, is copied into the parameter x. When the assignment x=x*x takes place, the only thing modified is the local variable x. The variable t, used to call sqr_it() will still have the value 10, and is unaffected by the operations inside the function. Hence the outputs will be 100 and 10.
Call-by-reference is the second way a subroutine can be passed arguments. In this method the address of an argument (not its values) is copied into the parameter. Inside the subroutine, this address is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the subroutine. In C++ it is possible to tell the compiler to automatically use call-by-reference rather than call-by-value for one or more parameters of a particular function. This can be accomplished with a reference parameter. When a reference parameter is used, the address (not the value) of an argument is automatically passed to the function. Within a function, operations on the reference parameters are automatically dereferenced. A reference parameter is declared by preceding the parameter name in the function's declaration with an &. Operations performed on a reference parameter affect the argument used to call the function, not the reference parameter itself.
Example
// Using reference parameter
#include <iostream.h>
void f(int &i);
main()
{
int val=1;
cout << "Old value for val is "<<val<<'n';
f(val);// pass address of val to f()
cout <<"New value for val is "<<val<<'n';
return 0;
}
void f(int &i)
{
i=10; // This modifies calling argument
}
This program displays the following output:
Old value for val is 1
New value for val is 10
Notice the declaration of i. It is preceded by an &, which causes it to become a
reference parameter. In side the function, the following statement
i=10
doesn't cause i to be given the value 10. Instead it causes the variable referenced by
i (in this case val) to be assigned the value 10.
Sine i has been declared as a reference parameter, the compiler will automatically
pass f() the address of any argument it is called with. Thus, in main() the
statement
f(val);
passes the address of val (not its value) to f(). There is no need to precede val with
& operator. Since f() receives the address of val() in the form of a reference, it may
modify the value of val.
Example
#include <iostream.h>
// Using reference parameter
void swap(int &x, int &y);
main()
{
int i,j;
i=10;
j=20;
cout << "initial values of i and j: ";
cout <<i<<' '<<j<<'n';
swap(j,i);
cout <<"swapped values of i and j ";
cout <<i<<' '<<j<<'n';
return 0;
}
/* Here, swap() is defined as using call-by reference, not call by value. Thus it can
exchange the two arguments it is called with */
void swap(int &x, int &y)
{
int temp;
temp=x;// save the value at address x
x=y; //put y into x
y=temp; //put x into y
}