// swapDemo.cpp
//
// Demonstrates a swap function.
// Demonstrates reference parameters.

#include <iostream>

using namespace std;

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

/*
 * int main()
 *
 * Displays values of two variables both before and
 * after the value of those variables are swapped
 * via a call to a swap function.
 *
 * Global variable:
 *    cout - standard output stream
 */
int main()
{
   // Announce purpose of program:
   cout << "This program will demonstrate "
             << "swapping two numbers." << endl;

   // Initialize and print two numbers:
   int n1 = 10;
   int n2 = 20;
   cout << "n1=" << n1 << " and n2=" << n2 << endl;

   // Swap the numbers:
   swap(n1, n2);

   // Print the numbers again, after swapping:
   cout << "n1=" << n1 << " and n2=" << n2 << endl;

   return 0;
}  // function main

/*
 * void swap(int& a, int& b)
 *
 * Swaps the values of two integer variables.
 *
 * Parameters:
 *    a - a reference to an integer.
 *       Preconditon:  a has been given any int value.
 *       Postcondition:  a has the previous value of b.
 *    b - a reference to another integer.
 *       Preconditon:  b has been given any int value.
 *       Postcondition:  b has the previous value of a.
 */
void swap(int& a, int& b)
{
   int temp = a;
   a = b;
   b = temp;
}  // function swap