// demonstrate squareByReference
cout << "z
= " << z << " before
squareByReference" << endl;
squareByReference( z );
cout << "z
= " << z << " after
squareByReference" << endl;
return 0;
// indicates successful termination
} // end main
//
squareByValue multiplies number by itself, stores the
//
result in number and returns the new value of number
int
squareByValue( int number )
{
return number *= number; // caller's argument not modified
} // end function squareByValue
//
squareByReference multiplies numberRef by itself and
//
stores the result in the variable to which numberRef
//
refers in function main
void
squareByReference( int &numberRef )
{
numberRef *= numberRef; // caller's argument modified
} // end function squareByReference
Changes number, but original
parameter (x) is not
modified.
Changes numberRef, which
is a reference to the variable being passed in. Thus, z is changed.