Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is start.cpp Purpose The purpose of this lab is to gain experience with pa

ID: 3590459 • Letter: B

Question

Below is start.cpp

Purpose The purpose of this lab is to gain experience with passing values by reference. You will modify one function and write another using this idea. Instructions 1. A piece of starter code called Start.cpp has been put on Canvas, start by downloading there should be no errors and it should run. If it does not, let your instructor know. 2. We want to convert the function addOne to instead of passing by value to the function to instead pass by reference, because of this the function will then return no values (in other words, the return type will be void). . Now that the function has been changed, uncomment the line of code in main that reads testAddOne(. This is a test that we wrote that when run will pass by reference a value to the function you have just converted. It will run a series of five tests against your function, any tests that pass will print an "OK" to the screen. After all tests have run, you should see a message saying “ALL addone TESTS PASS". If you do not see this message, there is a problem with your code. 4. Now we want you to wirite another function called minusone. We have given you the prototype for the function at the top of the code, notice that it takes a reference as an argument. Once you have the function written, uncomment both the testMinusOne

Explanation / Answer

Uncomment your testAddOne and testMinusOne functions completely..

and change the signatures of your addOne and minusOne functions to below:

void addOne(int &);
void minusOne(int &);


the function definitions will be below:


void addOne(int &x) {
x = x + 1;
}

void minusOne(int &x) {
x = x - 1;
}

================

Difference in passing by reference and passing by values:
When we pass by value, the method creates a new variable and keep the copy of the original value in new variable, hence anything which is modified inside the function, is done with the local copy of the variable which is inside function.. Hence the calling function will not see any chnages in the value of the variables, even if method makes any changes.,,

While with reference, the function refers to the original copy itself, and hence if the function makes any changes, that are propagated to the caller function and are persistent.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote