Answers for "copy by reference copy by value"

0

copy by reference copy by value

main() {
      int i = 10, j = 20;
      swapThemByVal(i, j);
      cout << i << "  " << j << endl;     // displays 10  20
      swapThemByRef(i, j);
      cout << i << "  " << j << endl;     // displays 20  10
      ...
   }

   void swapThemByVal(int num1, int num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

   void swapThemByRef(int& num1, int& num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }
Posted by: Guest on July-17-2020
0

copy by reference copy by value

main() {
      int i = 10, j = 20;
      swapThemByVal(i, j);
      cout << i << "  " << j << endl;     // displays 10  20
      swapThemByRef(i, j);
      cout << i << "  " << j << endl;     // displays 20  10
      ...
   }

   void swapThemByVal(int num1, int num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }

   void swapThemByRef(int& num1, int& num2) {
      int temp = num1;
      num1 = num2;
      num2 = temp;
   }
Posted by: Guest on July-17-2020

Code answers related to "copy by reference copy by value"

Browse Popular Code Answers by Language