Preface for Part 2 Before we start the 2nd exercise we need
Preface for Part 2:
Before we start the 2nd exercise we need to talk about call by value vs. call by reference.
When you pass parameters / arguments to a function they are passed by value. That means that your function gets a copy of the value from the calling function. Any changes your function makes to the argument that was passed by value are made to the copy and do not change the original variable in the calling function.
C++ has a feature where you can pass parameters / arguments by reference. When you pass by reference the function has access to the variables passed to it. When the function changes an argument that was passed by reference it changes the value of the original variable passed to the function.
Here is a standard prototype with pass by value:
int someFunction(double first, double second);
The function, someFunction, returns an int value. The parameters first and second parameters are both passed by value.
Here is the same prototype with a minor change:
int someFunction(double first, double &second);
The function still returns an int value. The first parameter is passed by value as it was before. The 2nd parameter, second, is now passed by reference – that is what the & after double tells the compiler. Now if someFunction changes second it will actually change the variable passed to someFunction.
Here is a quick example:
#include <iostream>
using namespace std;
int someFunction(double first, double &second);
int main()
{
int result;
double num1 = 5.0, num2 = 6.0;
result = someFunction(num1, num2);
cout << result << \" \" << num1 << \" \" << num2 << endl;
}
int someFunction(double first, double &second)
{
int res;
first++;
second++;
res = first + second;
return res;
}
The output will be as follows:
13 5 7
Note that num1 still has a value of 5 since the update in the function was to the local variable first. Variable first is a copy of num1. The value of num2 is now 7. The num2 variable in main was passed to someFunction by reference. When someFunction incremented second by 1 - the increment of second actually updated the num2 parameter passed by reference to someFunction.
Part 2: For your reference
Problem description
The default behavior for functions is to pass arguments / parameters by value. That means that a copy of the argument is sent to the function being called. Any changes you make to the parameter in the function is made to the copy and does not change the value of the argument in the calling function.
Here is some sample code that is using pass by value:
// Lab 9 Exercise 2
// Call by value and call by reference
//
// Program by: Place your name here
#include <iostream>
using namespace std;
// function prototype
// Swap the values value1 and value2
void swapInts(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;
swapInts(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 swapInts(int value1, int value2)
{
int temp;
// Swap value1 and value2
temp = value1;
value1 = value2;
value2 = temp;
return;
}
Here is the output:
Enter number 1 (enter -999 to quit processing): 42[Enter]
Enter number 2: 99[Enter]
Swapping numbers 1 (42) and 2 (99).
The new value of number 1 is 42.
The new value of number 2 is 99.
Enter number 1 (enter -999 to quit processing): -12[Enter]
Enter number 2: 12[Enter]
Swapping numbers 1 (-12) and 2 (12).
The new value of number 1 is -12.
The new value of number 2 is 12.
Enter number 1 (enter -999 to quit processing): -999[Enter]
Thank you for using this application.
Note that the output is not what we want. The values of num1 and num2 are the same after the call to swapInst as they were before the call to swapInts. Why is this the case? How do we fix it?
Why is this the case? The reason the values of num1 and num2 are unchanged in main is because they are passed to swapInts by value. As a result, the swapping of values done in the swapInts function is done to the copies that were passed to swapInts and not to the actual num1 and num2 variables in main.
How do we fix it? C++ has a feature where you can pass parameters / arguments by reference. When you pass by reference the function has access to the variables passed to it. When the function changes an argument that was passed by reference it changes the value of the original variable passed to the function.
Changes you need to make to the application
You need to change the swapInts and main functions of the exercise 2 code so that the two parameters value1 and value2 are passed by reference instead of being passed by value. Before proceeding you need to make sure you understand the difference between pass by value and pass by reference.
You will need to make changes to both the prototype and to the header of the swapInts function. No changes to main are needed. No changes are needed to the body of the swapInts function, the only changes needed are to the function prototype and to the function header.
After you have made your changes the output from exercise 2 should be as follows:
Enter number 1 (enter -999 to quit processing): 42[Enter]
Enter number 2: 99[Enter]
Swapping numbers 1 (42) and 2 (99).
The new value of number 1 is 99
The new value of number 2 is 42.
Enter number 1 (enter -999 to quit processing): -12[Enter]
Enter number 2: 12[Enter]
Swapping numbers 1 (-12) and 2 (12).
The new value of number 1 is 12.
The new value of number 2 is -12.
Enter number 1 (enter -999 to quit processing): -999[Enter]
Thank you for using this application.
Note that the values for number 1 and number 2 (variables num1 and num2 in main) have been swapped. In the before the first call to swapInts number 1 had a value of 42 and number 2 had a value of 99. After the call to swapInts number1 has a value of 99 and number 2 has a value of 42.
Solution
// Call by value and call by reference
//
// Program by: Place your name here
#include <iostream>
using namespace std;
// function prototype
// Swap the values value1 and value2
void swapInts(int &value1, int &value2); //Call by Reference prototype
// 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;
swapInts(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;
}
//Give & before the variable name. Now it become a reference variable. Whatever changes made to this variable inside this function
//will reflect in the actual parameter of the function call
void swapInts(int &value1, int &value2)
{
int temp;
// Swap value1 and value2
temp = value1;
value1 = value2;
value2 = temp;
return;
}
Output:
Enter number 1 (enter -999 to quit processing): 42
Enter number 2: 99
Swapping numbers 1 (42) and 2 (99).
The new value of number 1 is 99.
The new value of number 2 is 42.
Enter number 1 (enter -999 to quit processing): -12
Enter number 2: 12
Swapping numbers 1 (-12) and 2 (12).
The new value of number 1 is 12.
The new value of number 2 is -12.
Enter number 1 (enter -999 to quit processing): -999
Thank you for using this application.




