/******************************************************* Author: Francesco Taurisano (www.ftlaboratory.com) Parameters.cpp Parameter passing methods: by value and by reference *******************************************************/ #include using namespace std; // Parameter passing by value // a and b are formal parameters void exchange1(int a, int b) { int z; z=a; a=b; b=z; } // Parameter passing by reference (first way) // a and b are formal parameters void exchange2(int *a, int *b) { int z; z=*a; *a=*b; *b=z; } // Parameter passing by reference (second way) // a and b are formal parameters void exchange3(int& a, int& b) { int z; z=a; a=b; b=z; } int main() { // Actual parameters // x and y are actual parameters int x, y; cout << "Parameter passing methods" << endl; cout << "Insert x "; cin >> x; cout << "Insert y "; cin >> y; // Parameter passing by value exchange1(x,y); // Show x and y cout << "Effect of parameter passing by value" << endl; cout << "x=" << x << endl; cout << "y=" << y << endl; // Parameter passing by reference (first way) exchange2(&x,&y); // Show x and y cout << "Effect of parameter passing by reference (first way)" << endl; cout << "x=" << x << endl; cout << "y=" << y << endl; // Parameter passing by reference (second way) exchange3(x,y); // Show x and y cout << "Effect of parameter passing by reference (second way)" << endl; cout << "Note: because this is the second inversion, the parameters return to their original values" << endl; cout << "x=" << x << endl; cout << "y=" << y << endl; system("pause"); return 0; }