GATE 2001 | Parameter Passing What is printed by print statements in the program P1 assuming call by referencβ¦
ravikaranrathoreΒ·
GATE 2001 | Parameter Passing
What is printed by print statements in the program P1 assuming call by reference parameter passing? Program P1(){ x = 10; y = 3;
func1(y,x,x);
print x;
print y;
}
func1(x,y,z){
y = y+4; // line 1
z = x+y+z; // line 2
} A. 10,3 B. 31,3 C. 27,3 D. None of the above
So, in this the comments are marked by my. and catch is there only for correct answer. AI mentor says answer is C, answer key gave ans. B when i said the same to AI mentor, it also changed its answer. Pls without using AI tell me actual rule, line 1, will update local y value and since this local y is referenced to x in program p1, hence x is also updated to 14.
line 2, z = 3 + 14 + z what value of z we use now,, since when parameters were passed z was initialised as 10 (by x from p1),, now when y updated x in line 1,, It should not affect z here right? but answer key's correct answer come, when we use updated value. AI is causing more confusion, so please give me resource of the rule too. Thanks.
@ravikaranrathore The most important rule in call-by-reference is that you are not passing values; you are giving the function new names(or labels) for the original memory boxes. When P1 calls func1(y, x, x), hereβs what happens: func1's parameter x becomes another label for P1's y box. func1's parameter y becomes another label for P1's x box. func1's parameter z also becomes another label for P1's x box. This is the critical point: y and z inside func1 are two different labels pointing to the exact same memory box. Now let's follow the execution with this in mind. Initial State: Original x box contains: 10 Original y box contains: 3 1. y = y + 4; The y on the left refers to the original x box. The y on the right also refers to the original x box. The operation reads the current value from the original x box (10), adds 4, and stores the result (14) back into that same box. State after Line 1: The original x box now contains 14. The original y box is untouched. 2. z = x + y + z; This is where the confusion happens. You don't use the values from when the function was called. You use the current values from the memory boxes the labels point to. x (on the right): This label points to the original y box. Its value is 3. y (on the right): This label points to the original x box. Its value was just updated in the previous step, so its current value is 14. z (on the right): This label also points to the original x box. So, its current value is also 14. The parameter z doesn't "remember" the value 10. It's just a live link to the x memory box, and it will always fetch whatever value is currently in there. The Calculation: 3 + 14 + 14 = 31. The Assignment: The result (31) is stored in the box that z points to, which is the original x box. State after Line 2: The original x box now contains 31. Final Result: print x; prints the final value in the original x box: 31. print y; prints the value in the original y box, which was never changed: 3.