Advertisement
gha890826

Difference of Call By Value, Call By Reference, Address

Mar 29th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void cbv(int, int);
  5. void cbr(int&, int&);
  6. void cba(int*, int*);
  7.  
  8.  
  9.  
  10. int main()
  11. {
  12.     cout << "ROUND 1:\n";
  13.     int a = 1, b = 2;
  14.     cout << "\ta and b in main() at first:\n\ta=" << a << "\tb=" << b << "\n\t" << &a << '\t' << &b << "\n\n";
  15.     cbv(a, b);
  16.     cout << "\ta and b in main() after Call By Value\n\ta=" << a << "\tb=" << b << "\n\t" << &a << '\t' << &b << "\n\n";
  17.  
  18.     cout << "ROUND 2:\n";
  19.     a = 1;
  20.     b = 2;
  21.     cout << "\ta and b in main() at first:\n\ta=" << a << "\tb=" << b << "\n\t" << &a << '\t' << &b << "\n\n";
  22.     cbr(a, b);
  23.     cout << "\ta and b in main() after Call By Reference\n\ta=" << a << "\tb=" << b << "\n\t" << &a << '\t' << &b << "\n\n";
  24.  
  25.     cout << "ROUND 3:\n";
  26.     a = 1;
  27.     b = 2;
  28.     cout << "\ta and b in main() at first:\n\ta=" << a << "\tb=" << b <<"\n\t"<<&a<<'\t'<<&b<< "\n\n";
  29.     cba(&a, &b);
  30.     cout << "\ta and b in main() after Call By Address\n\ta=" << a << "\tb=" << b << "\n\t" << &a << '\t' << &b << "\n\n";
  31. }
  32.  
  33. void cbv(int i, int j)
  34. {
  35.     int temp = i;
  36.     i = j;
  37.     j = temp;
  38.     cout << "\tin CallByValue()\n\ti=" << i << "\tj=" << j << "\n\t" << &i << '\t' << &j << "\n\n";
  39. }
  40. void cbr(int &i, int &j)
  41. {
  42.     int temp = i;
  43.     i = j;
  44.     j = temp;
  45.     cout << "\tin CallByReference()\n\ti=" << i << "\tj=" << j << "\n\t" << &i << '\t' << &j << "\n\n";
  46. }
  47. void cba(int *i, int *j)
  48. {
  49.     int temp = *i;
  50.     *i = *j;
  51.     *j = temp;
  52.     cout << "\tin CallByAddress()\n\ti=" << *i << "\tj=" << *j << "\n\t" << &i << '\t' << &j <<"\n\t" << i << '\t' << j <<  "\n\n";
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement