//------------------------------------------------------ // // swap.cpp // // The classic program to illustrate reference parameters. // // Written by: Simon Parsons // 27th October 2008 // //------------------------------------------------------ // Header files and namespace. #include using namespace std; // Function prototypes void swap( int &a, int &b ); void notswap( int a, int b ); // Main program int main() { int x = 5; int y = 7; // With reference parameters we swap the values of x and y. cout << "Before swap: x = " << x << " y = " << y << endl; swap( x, y ); cout << "...after swap: x = " << x << " y = " << y << endl; // Without reference parameters, we don't swap x and y. cout << "Let's try that again" << endl; x = 23; y = 6; cout << "Before swap: x = " << x << " y = " << y << endl; notswap( x, y ); cout << "...after swap: x = " << x << " y = " << y << endl; } // Declare function swap(), using reference parameters // This one swaps the variable values. void swap( int &a, int &b ) { int tmp; tmp = a; a = b; b = tmp; return; } // Declare function notswap() // This does not swap the values. void notswap( int a, int b ) { int tmp; tmp = a; a = b; b = tmp; return; }