// Zig Herzog
// Oct. 23, 2007
// Example of passing multiple values back and forth using
// the method "pass-by-reference"
// Note that the first two paramters are passed by reference , the third
// one by value. In function main() the variable "control" is merely
// printed to show that passing-by-value is NOT a two-way street.
#include <iostream>
using namespace std ;
void ex2 ( int& , int& , int ) ; // <<<< prototyping
void flip ( int& , int& ) ;
int main ()
{
int v1 , v2 , control ;
cout << "Give two integer values to be ordered : " ;
cin >> v1 >> v2 ;
cout << "Return in which order, 0=increasing 1=decreasing : " ;
cin >> control ;
ex2 ( v1 , v2 , control ) ; // <<<<< call to function ex1()
cout << "v1=" << v1 << " v2=" << v2 << " control=" << control << endl ;
}
////////////// Definition of function ex2() ////////////////////////
void ex2 ( int& v , int& w , int cc )
{
int tmp ; // local variable
if ( cc == 0 )
{ // Increasing order
if ( v > w )
{
flip ( v , w ) ; // flip values
}
}
else
{ // decreasing order
if ( v < w )
{
flip ( v , w ) ; // flip values
}
}
cc = 9999 ; // Just to show that "passing by value" will not
// effect variable "control" in the calling program.
}
////////////// Definition of function flip() ////////////////////////
// Exchanging the values of its arguments
void flip ( int& v , int& w )
{
int tmp ;
tmp = v ;
v = w ;
w = tmp ;
}
Zig Herzog; hgn@psu.edu