Home Segments Index Top Previous Next

559: Practice

The type of a call-by-reference parameter does not need to be a class. Call-by-reference parameters can be, for example, of type int or double. Predict the output obtained from the following program:

#include   
int call_by_value_square (int m) { 
  m = m * m; 
  return m; 
} 
int call_by_reference_square (int& m) { 
  m = m * m; 
  return m; 
} 
main ( ) { 
  int n = 5; 
  cout << "n's value is " << n << endl; 
  cout << "The call-by-value square of n is " 
       << call_by_value_square (n) << endl;   
  cout << "n's value is " << n << endl; 
  cout << "The call-by-value square of n+1 is "  
       << call_by_value_square (n + 1) << endl; 
  cout << "n's value is " << n << endl; 
  cout << "The call-by-reference square of n is "  
       << call_by_reference_square (n) << endl;   
  cout << "n's value is " << n << endl; 
  cout << "The call-by-reference square of n+1 is " 
       << call_by_reference_square (n + 1) << endl; 
  cout << "n's value is " << n << endl; 
}