GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 18

Tracing value parameters and successive assignments

Programming in C · Functions & Parameter Passing · Full GATE CSE 2019 paper

GATE 2019, Q18 1 mark · NAT
Report issue

Functions & Parameter Passing

Question 18, GATE 2019: Function jumble(int x,int y) assigns x=2*x+y and returns x. Main initializes x=2,y=5, then executes y=jumble(y,x), then x=jumble(y,x), and finally prints main's x. Find the printed integer.
Tap to enlarge
Answer & solution

Official answer: 26 to26

Our answer: 26

Method: keep the function's local parameter names separate from the caller's variables, and evaluate statements in sequence.

1. Initially main has (x,y)=(2,5). In y=jumble(y,x), the first argument is main's y=5 and the second is main's x=2. Inside the function, local (x,y) therefore starts as (5,2).
2. The function computes local x=2×5+2=12 and returns 12. C passes these integers by value, so changing the local x does not alter main's x. The assignment updates only main's y, leaving main at (2,12).
3. The second call, x=jumble(y,x), now passes (12,2). The returned value is 2×12+2=26, and the enclosing assignment stores it in main's x.
4. Main is now (26,12), so printing x displays 26. Reusing the function's parameter name x does not make it an alias of the caller's x.

Result: 26. The official range 26 to 26 accepts this exact value.

Original paper, page 7 · Answer key