fork download
  1. #include <iostream>
  2. using namespace std;
  3. // Function prototype
  4. void swap(int&, int&);
  5. int main()
  6. {
  7. int a = 1, b = 2;
  8. cout << "Before swapping" << endl;
  9. cout << "a = " << a << endl;
  10. cout << "b = " << b << endl;
  11. swap(a, b);
  12. cout << "\nAfter swapping" << endl;
  13. cout << "a = " << a << endl;
  14. cout << "b = " << b << endl;
  15. return 0;
  16. }
  17. void swap(int& n1, int& n2) {
  18. int temp;
  19. temp = n1;
  20. n1 = n2;
  21. n2 = temp;
  22. }
Success #stdin #stdout 0s 4152KB
stdin
Standard input is empty
stdout
Before swapping
a = 1
b = 2

After swapping
a = 2
b = 1