Mega Code Archive

 
Categories / C++ Tutorial / Function
 

Compare pass-by-value and pass-by-reference with references

#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