//swapper //reference parameters vs value parameters //function overloading #include //for cin, cout, endl using namespace std; //a function with value parameters void badswap(int x, int y); //a function with reference parameters void goodswap(int &x, int &y); // could be // int& x, OR int & x, OR int &x void goodswap(float &x, float &y); // overloading goodswap int main() { int a=5; int b=10; cout << "Before function calls:\n" << "a= " << a << " b= " << b << endl; badswap(a, b); cout << "After call to badswap:\n" << "a= " << a << " b= " << b << endl; goodswap(a, b); cout << "After call to goodswap:\n" << "a= " << a << " b= " << b << endl; badswap(2,50); //goodswap(2,5);//where do the labels x and y go? //Now for floats float p=3.14; float q=2.7; cout << "Before float function calls:\n" << "p= " << p << " q= " << q << endl; goodswap(p,q); //which function is called? cout << "After call to float goodswap:\n" << "p= " << p << " q= " << q << endl; badswap(p,q); //what happens here? return 0; } void badswap(int x, int y) { int temp; temp=x; x=y; y=temp; cout << "At end of badswap, before return:\n" << "x= " << x << " y= " << y << endl; return; } void goodswap(int &x, int &y) { int temp; temp=x; x=y; y=temp; cout << "At end of goodswap, before return:\n" << "x= " << x << " y= " << y << endl; return; } void goodswap(float &x, float &y) { float temp; temp=x; x=y; y=temp; cout << "At end of goodswap for floats," << "before return:\n" << "x= " << x << " y= " << y << endl; return; }