Question: Lab 6.6 Using Value and Reference Parameters Below is a copy of the source code. 1 // Lab 6 swapNums.cpp -- Using Value and Reference
Lab 6.6
Using Value and Reference Parameters
Below is a copy of the source code.
1 // Lab 6 swapNums.cpp --
Using Value and Reference Parameters
2 // This program uses a function to swap the values in two variables
.
3 // PUT YOUR NAME HERE.
4 #include
5 using namespace std;
6
7 // Function prototype
8 void swapNums(int, int);
9
10 /***** main *****/
11 int main()
12 {
13 int num1 = 5,
14 num2 = 7;
15
16 // Print the two
variable values
17 cout << "In main the two numbers are "
18 << num1 << " and " << num2 << endl;
19
20 // Call a function to swap the values stored
21 // in the two variables
22 swapNums(num1, num2);
23
24 // Print the same two variable values again
25 cout << "Back in main again the two numbers are "
26 << num1 << " and " << num2 << endl;
27
28 return 0;
29 }
30
31 /***** swapNums *****/
32 void swapNums(int a, int b)
33 { // Parameter a receives num1 and parameter b receives num2
34 // Swap the values that came into parameters a and b
35 int temp = a;
36 a = b;
37 b = temp;
38
39 // Print the swapped values
40 cout << "In swapNums, after swapping, the two numbers are "
41 << a << " and " << b << endl;
42 }
The question I need an answer for is:
Step 3:
Change the two swapNums parameters to be reference variables. Section 6.13 of your text shows how to do this. You will need to make the change on both the function header and the function prototype. Nothing will need to change in the function call. After making this change, recompile and rerun the program. If you have done this correctly, you should get the following output.
In main the two numbers are 5 and 7
In swapNums, after swapping, the two numbers are 7 and 5
Back in main again the two numbers are 7 and 5
Explain what happened this time. _______________________________________________________
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
