+ 2
What is the output?
void Fun(int &i,int j) { i+=3; j*=2; } void main() { int a=0,b=2; Fun(a,b); cout<<a<<”,”<<b<<endl; } A、3,4 B、3,2 C、0,4 D、0,2 I can't tell the difference between "&i" and "j". I wanted to choose "B" as the correct answer.
7 Réponses
+ 5
i += x is short for i = i + x.
i += x is short for i = i * x.
The value of x gets added to i and that becomes the new value of i.
a = 3 and b = 2 so,
0 = 0 + 3 , variable become 3.
2 = 2 * 2, variable becomes 4.
+ 5
Ah that makes sense since the second parameter doesn't take the address of b. I thought that was just a typo because you said the answer was 'A' haha.
+ 3
I think the correct answer should be "B" (3,2) because the function's ampersand causes a permanent change in the value of "a" (as it passes through "&i") but not in "b".
So by the time you cout "b" it has already returned to its original value (2).
+ 1
Oh,the answer is "B".I have tried it myself.I have understood a little.
+ 1
Actually, the correct answer is _not_ A as you claim, but B. and if your teacher/lecturer said so, then he/she is wrong!
It works like this:
Variable 'a' is passed by reference to Fun, so any changes in i inside Fun is reflected back to 'a' (inside Fun i is actually a pointer to 'a' - a reference is just a kind of fancy pointer).
'b' on the other hand is passed by value to argument j of Fun. So j is initialized with the value of 'b', but j and b are in fact completely separate variables (unlike i which is a pointer/reference to a).
So the changes to i inside Fun is in fact done to 'a' through pointer/reference i, whilst 'b' is not affected by Fun. That gives the answer B 3,2.
But, if you still have doubts, why don't you try the code in the Code Playground and see for yourself!
"To measure is to know!"
+ 1
Try to rewrite te code as following and you will see the values of a, b, i and j. Change the values of "a" and "b" and you will see that "i" is always equals to "a" (in fact "i" changes the "a" value), that "b" does not change and "j" is always the double of "b".
#include <iostream>
using namespace std;
void Fun(int *i, int j) { // declares the function "Fun" with a variable "i" as an integer pointer and a variable "j" as an integer
*i += 3; // add 3 to the value stored at the address pointed by the pointer "i" (in this case does' t matter the pre/post operation because it is a self applying operation)
j *= 2; // multiply "j" by 2 (in this case does' t matter the pre/post operation because it is a self applying operation)
cout << "i = " << *i << ", j = " << j; // Show the values of "i" and "j"
}
int main () {
int a = 0, b = 2; //declare and load the variables "a" as 0 and "b" as 2
Fun (&a, b) // call the function "Fun" passing the address of "a" as first parameter and the value of "b" as the second parameter
cout << "a = " << a << ", b = " << b; // Show the values of "a" and "b"
}
For a = 0 and b = 2 -> a = 3, b = 2, i = 3 and j = 4.
For a = 5 and b = 7-> a = 8, b = 7, i = 8 and j = 14.
Try others values to "a" and "b" and try to figure out what is going on.
0
Actually,the correct answer is "A".Why?