Call by value and call by reference

I need to change the swap and main function of the exercise 2 code so that the two parameters value1 and value2 are passed by reference instead of being passed by value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

// function prototype
// Swap the values value1 and value2
void swap(int value1, int value2);

// main function - you application starts here
int main()
{
   int num1 = 0, num2;

   do 
   {
      // ask for the first input value
      cout << "Enter number 1 (enter -999 to quit processing): ";
      cin >> num1;

      // check to see if we should ask for num2 and then swap num1 and num2
      if (num1 != -999)
      {
         // get num2
         cout << "Enter number 2: ";
         cin >> num2;
         // OK, we now swap the values
         cout << "Swapping numbers 1 (" << num1 << ") and 2 (" << num2 <<")." << endl;
         swap(num1, num2);
         cout << "The new value of number 1 is " << num1 << "." << endl;
         cout << "The new value of number 2 is " << num2 << "." << endl;
      }
   }
   while (num1 != -999);

   cout << "Thank you for using this application." << endl;
}

// This function is supposed to swap the values value1 and value2, but it 
// will not work because value1 and value2 are passed by value.
void swap(int value1, int value2)
{
   int temp;

   // Swap value1 and value2
   temp = value1;
   value1 = value2;
   value2 = temp;

   return;
}


Change line 7 to:
void swap(int& value1, int& value2);

Change line 40 to:
void swap(int& value1, int& value2)

Remove line 49.
What's wrong with line 49?
Nothing. It's my OCD.
What's wrong with line 49?

It's useless. It's a line of code doing nothing.

Funnily enough the OP doesn't have a return statement in main().
Topic archived. No new replies allowed.