// Copyleft: Sathiamoorthy Manoharan. #include // Passing by value means passing a copy of the object. // This version of Swap passes n1 and n2 by value (i.e. copy of the // objects 'a' and 'b' are passed in as 'n1' and 'n2'. // Changes to 'n1' and 'n2' inside the method do not // change the original objects 'a' and 'b'. int SwapV1(int n1, int n2) { int temp = n1; n1 = n2; n2 = temp; } // SwapV1 // Passing by reference means passing the object itself. // This version of Swap passes n1 and n2 by reference (i.e. the // objects 'a' and 'b' are passed, though they are named 'n1' and // 'n2' within the method. int SwapV2(int& n1, int& n2) { int temp = n1; n1 = n2; n2 = temp; } // SwapV2 int main() { int a = 34, b = 86; std::cout << "Initial values:\n"; std::cout << "a: " << a << " b: " << b << "\n"; SwapV1(a, b); std::cout << "After SwapV1:\n"; std::cout << "a: " << a << " b: " << b << "\n"; SwapV2(a, b); std::cout << "After SwapV2:\n"; std::cout << "a: " << a << " b: " << b << "\n"; return 0; }