In Visual Studio Code under labactivity8_1 folder, create a new file main.cpp. Open the C++ source file main.cpp in the text editor and copy the source code below. /** * @file WRITE FILE NAME * @author WRITE STUDENT NAME(S) * @brief Using value and reference parameters. This program * uses a function to swap the values in two variables. * @date WRITE DATE TODAY * */ #include using namespace std; // Function prototype void swapNums(int number1, int number2); int main() { int num1 = 5, num2 = 7; // Print the two variable values cout << "In main the two numbers are " << num1 << " and " << num2 << endl; // Call a function to swap the values stored // in the two variables swapNums(num1, num2); // Print the same two variable values again cout << "Back in main again the two numbers are " << num1 << " and " << num2 << endl; return 0; } /** * @brief WRITE DESCRIPTION OF THE FUNCTION * * @param number1 DESCRIPTION * @param number2 DESCRIPTION */ void swapNums(int number1, int number2) { // Parameter a receives num1 and parameter b receives num2 // Swap the values that came into parameters a and b int temp = number1; number1 = number2; number2 = temp; // Print the swapped values cout << "In swapNums, after swapping, the two numbers are " << number1 << " and " << number2 << endl; } Update the header comment with the correct information. Read the source code, paying special attention to the swapNums parameters. When the program is run do you think it will correctly swap the two numbers? Compile and run the program to find out. Explain what happened. It swapped the numbers in the function but did not return any values so the numbers won’t get swapped in main. 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. The function was passed references to the variables in main. This allows the function to change the contents of the variables in main as it is working on the same variable address in memory. Update the function header comments of swapNums.
Lab 8.1 - Using Value and Reference Parameters
-
In Visual Studio Code under labactivity8_1 folder, create a new file main.cpp. Open the C++ source file main.cpp in the text editor and copy the source code below.
/** * @file WRITE FILE NAME * @author WRITE STUDENT NAME(S) * @brief Using value and reference parameters. This program * uses a function to swap the values in two variables. * @date WRITE DATE TODAY * */ #include <iostream> using namespace std; // Function prototype void swapNums(int number1, int number2); int main() { int num1 = 5, num2 = 7; // Print the two variable values cout << "In main the two numbers are " << num1 << " and " << num2 << endl; // Call a function to swap the values stored // in the two variables swapNums(num1, num2); // Print the same two variable values again cout << "Back in main again the two numbers are " << num1 << " and " << num2 << endl; return 0; } /** * @brief WRITE DESCRIPTION OF THE FUNCTION * * @param number1 DESCRIPTION * @param number2 DESCRIPTION */ void swapNums(int number1, int number2) { // Parameter a receives num1 and parameter b receives num2 // Swap the values that came into parameters a and b int temp = number1; number1 = number2; number2 = temp; // Print the swapped values cout << "In swapNums, after swapping, the two numbers are " << number1 << " and " << number2 << endl; } -
Update the header comment with the correct information.
-
Read the source code, paying special attention to the swapNums parameters. When the program is run do you think it will correctly swap the two numbers? Compile and run the program to find out.
Explain what happened. It swapped the numbers in the function but did not return any values so the numbers won’t get swapped in main.
-
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 5Explain what happened this time. The function was passed references to the variables in main. This allows the function to change the contents of the variables in main as it is working on the same variable address in memory.
-
Update the function header comments of swapNums.
Step by step
Solved in 4 steps with 2 images