Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void call_by_value_swap(int v1, int v2) {
  4. //std::cout << "Address of v1, v2 is (" << &v1 << ", " << &v2 << ")" << std::endl;
  5. int temp = v1;
  6. v1 = v2;
  7. v2 = temp;
  8. }
  9.  
  10. void call_by_address_swap(int *v1, int *v2) {
  11. //std::cout << "Address of v1, v2 is (" << v1 << ", " << v2 << ")" << std::endl;
  12. int temp = *v1;
  13. *v1 = *v2;
  14. *v2 = temp;
  15. }
  16.  
  17. void call_by_reference_swap(int &v1, int &v2) {
  18. //std::cout << "Address of v1, v2 is (" << &v1 << ", " << &v2 << ")" << std::endl;
  19. int temp = v1;
  20. v1 = v2;
  21. v2 = temp;
  22. }
  23.  
  24. int main (int argc, char *argv[]) {
  25. int x = 1;
  26. int y = 0;
  27.  
  28. std::cout << "Origin x, y is (" << x << ", " << y << ")" << std::endl;
  29. //std::cout << "Origin address of x, y is (" << &x << ", " << &y << ")" << std::endl;
  30.  
  31. std::cout << std::endl;
  32.  
  33. call_by_value_swap(x, y);
  34. std::cout << "call_by_value x, y is (" << x << ", " << y << ")" << std::endl;
  35.  
  36. std::cout << std::endl;
  37.  
  38. x = 1;
  39. y = 0;
  40. call_by_address_swap(&x, &y);
  41. std::cout << "call_by_address x, y is (" << x << ", " << y << ")" << std::endl;
  42.  
  43. std::cout << std::endl;
  44.  
  45. x = 1;
  46. y = 0;
  47. call_by_reference_swap(x, y);
  48. std::cout << "call_by_reference x, y is (" << x << ", " << y << ")" << std::endl;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement