// trySwap5.cc   P. Conrad 2/13/2007
// Here is ANOTHER way to make an effective swap function
// This is C++ style pass-by-reference.

// This is "true" pass-by-reference
// Here, the formal parameter becomes an "alias" of the actual 
// parameter.

#include <iostream>
using namespace std;

// WARNING: this ampersand does NOT mean "address of"
// It means "alias for".

void swap(int &a, int &b)
{
  // a and b are formal parameters

  cout << "Before swap a = " << a << " b=" << b << endl;

  int temp = a;
  a = b;
  b = temp;

  cout << "After swap a = " << a << " b=" << b << endl;

  return ;
}

int main()
{

  int x; int y;

  x=4; y=7;

  cout << "x = " << x << " y=" << y << endl;

  swap(x,y); // x and y are actual parameters
  
  cout << "x = " << x << " y=" << y << endl;
  cout << "Sweet! It worked!\n";

  return 0;

}

