swap.cpp
Demo in class April 22, 2008
//Program to demonstrate
call-by-reference parameters.
#include <iostream>
using namespace std;
void getNumbers(int& input1, int& input2);
//Reads two integers from the keyboard.
void swapValuesValue(int variable1, int variable2);
//Interchanges the values of variable1 and variable2.
//Call-by-value
void swapValuesReference(int& variable1, int& variable2);
//Interchanges the values of variable1 and variable2.
//Call-by-reference
void showResults(int output1, int output2);
//Shows the values of variable1 and variable2, in that order.
int main( )
{
int firstNum, secondNum;
getNumbers(firstNum, secondNum);
swapValuesValue(firstNum, secondNum);
showResults(firstNum, secondNum);
swapValuesReference(firstNum, secondNum);
showResults(firstNum, secondNum);
cout << endl;
return 0;
}
void getNumbers(int& input1, int& input2)
{
cout << "\nEnter two integers: ";
cin >> input1
>> input2;
}
void swapValuesValue(int variable1, int variable2)
{
int temp;
cout << "\nEntering Swap by Value:\n";
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
void swapValuesReference(int& variable1, int& variable2)
{
int temp;
cout << "\nEntering Swap by Reference:\n";
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
void showResults(int output1, int output2)
{
cout << "In reverse order the numbers are: "
<< output1
<< " " << output2 << endl;
}