0
Can get some help in answering this question. What is the output of the following code?
#include <iostream> using namespace std; int update(int & temperature) { temperature + 1; return 0; } int main () { int temp = 42; update(temp); cout << temp << endl; }
3 ответов
0
As we all know that the execution of the program starts from the main function. In this case, when the
program is executed the first thing is gets is the assigning of variable named "temp" as a integer 42.
After this, we can see a function named "update" is called; we then look for this "update" function and see what its doing. It is taking a parameter and the value is passed by reference. In the main we are passing the value of "temp" which is 42.
When the value 42 is taken by "update" function it performs calculation but returns the value 0. Moving to the next line of code in the main function, it consoles out the value of "temp" , thus since the original value is not updated the value which will be output is 42.
+ 2
43
+ 2
YOU ARE PASSING BY REFERENCE.
MEANS CHANGING PARAMETER CHANGES ARGUMENT VALUE.