Call By value and Reference

#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; 
 }
 void squareByReference( int &cRef )
 {
 cRef *= cRef; // caller's argument modified
 }
OUTPUT:
       x = 2 before squareByValue
             Value returned by squareByValue: 4
       x = 2 after squareByValue
       z = 4 before squareByReference
       z = 16 after squareByReference

No comments:

Post a Comment