Handout - Swap function (reference parameters)


/*			Swap Function
 *
 *	Demonstrates the use of reference parameters
 */

#include <iostream>

using namespace std;

void swap (int& a, int& b);     // prototype 

int main ()
{

  int x = 3,
      y = 5;

  cout << "Before call to swap, x =" << x << " and y =" << y << endl;
  swap (x, y);
  cout << "After call to swap, x =" << x << " and y =" << y << endl;

  return 0;
}

//
//  PRE:   a and b are integers
//  POST:  the values of a and b have been swapped
//
void swap (int& a, int& b)
{
  int temp;

  temp = a;
  a =b;
  b = temp;	
}