<< Chapter < Page | Chapter >> Page > |
Note:
Two ways to invoke functions in many programming languages are: call by value and
call by reference
When an argument is passed call by value , a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect the original variable’s value in the caller.
With call-by-reference , the caller gives the called function the ability to access the caller’s data directly, and to modify that data if the called function chooses so.
To indicate that the function parameter is passed-by-reference , simply follow the parameter’s type in the function prototype of function header by an ampersand (&).
For example, the declaration
int&count;
in the function header means “count is a reference parameter to an int”.
Example
// Comparing call-by-value and call-by-reference with references.
#include<iostream.h>
int squareByValue( int );
void squareByReference( int&);
int main()
{
int x = 2, z = 4;
cout<<"x = "<<x<<" before squareByValue\n"
<<"Value returned by squareByValue: "
<<squareByValue( x )<<endl
<<"x = "<<x<<" after squareByValue\n"<<endl;
cout<<"z = "<<z<<" before squareByReference"<<endl;
squareByReference( z );
cout<<"z = "<<z<<" after squareByReference"<<endl;
return 0;
}
int squareByValue( int a )
{
return a *= a; // caller's argument not modified
}
void squareByReference( int&cRef )
{
cRef *= cRef; // caller's argument modified
}
The output of the above program:
x = 2 before squareByValue
Value returned by squareByValue: 4
x = 2 after squareByReference
z = 4 before squareByReference
z = 16 after squareByReference
Since cRef is a reference parameter, so, squareByReference() now has direct access to the argument z. Thus, any change to cRef within squareByReference() directly alters the value of z in main(). The assignment of value to cRef within squareByReference() is relected in main() as the altering of z’s value.
Recall from Chapter 2 that the ampersand,&, in C++ means “the address of”. Additionally, an&symbol used within a declaration refers to “the address of” the preceding data type. Using this information, declaration such as double&num1 and int&secnum are sometimes more clearly understood if they are read backward. Reading the declaration int&cRef in this manner yields the information that “cRef is the address of an int value.”
Example
// This program can solve quadratic equation
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
in quad( double, double, double, double&, double&);
int main()
{
double a, b, c, x1, x2;
int code;
cout<<“Enter the coefficients of the equation: “<<endl;
cin>>a>>b>>c;
code = quad(a, b, c, x1, x2);
if (code == 1 || code == 2)
cout<<"x1 = “<<x1<<setw(20)<<“x2 = “<<x2<<endl;
else if (code == 3)
cout<<“ There is no solution “<<endl;
return 0;
}
int quad(double a, double b, double c, double&px1, double&px2)
{
double del;
del = b*b – 4.0*a*c;
if (del == 0.0)
{
px1 = -b/(2*a);
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?