Compare pass-by-value and pass-by-reference with references : function definition « Function « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

int byValue( int ); 
void byReference( int & ); 
                                                   
int main()
{
   int x = 2; 
   int z = 4; 

   cout << byValue( x ) << endl;  
   cout << "x = " << x << endl;

   byReference( z );
   cout << "z = " << z << endl;
   return 0;
} 

int byValue( int number )
{
   return number *= number;
}
void byReference( int &numberRef )
{
   numberRef *= numberRef; 
}
4
x = 2
z = 16








7.1.function definition
7.1.1.This program contains two functions: main() and f().
7.1.2.Define function to add two parameters together
7.1.3.The function call stack and activation records
7.1.4.Functions that take no arguments.
7.1.5.Compare pass-by-value and pass-by-reference with references
7.1.6.Default arguments with parameters
7.1.7.Definition of function template maximum
7.1.8.Functions in an expression: order of evaluation