GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 42

Reference parameter and multiplication evaluation order

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 2013 paper

GATE 2013, Q42 2 marks · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 42, GATE 2013: p starts 5. The function uses reference parameter x and value parameter c: int f(int &x, int c){c=c−1;if(c==0)return1;x=x+1;return f(x, c)*x;} What does f(p, p) return? A3024, B6561, C55440, D161051. The published key awards Marks to All; source does not specify operand evaluation order.
Tap to enlarge
Your answer
Answer & solution

Official answer: Marks to All

Our answer: No uniquely specified value; 3024 (A) and 6561 (B) arise from different operand orders.

The parameter declaration int &x is C++ reference syntax, not valid C. Every recursive invocation aliases the same p through x, whereas c is a private by-value copy. The four non-base calls increment p successively to 6, 7, 8 and 9; the fifth call returns 1 without incrementing p. In return f(x, c) * x, multiplication does not require the left operand to be evaluated first. If each call reads its right-hand x before executing the recursive call, the stored factors are 6, 7, 8, 9 and the return value is 6×7×8×9=3024 (A). If each call executes the recursive call before reading its right-hand x, all four factors are the final p=9 and the result is 9^4=6561 (B). Mixed choices can give other values. Under C++ rules sequencing a function’s execution as a unit relative to otherwise unsequenced outside evaluations, this is an unspecified operand-order issue; the recursive function call must not be confused with an unsequenced x++ written directly beside a read of x. The source does not prescribe an evaluation convention or a language version, so no unique numerical option follows. The official Code A key explicitly awards Marks to All. This record is classified under compiler parameter-passing/evaluation semantics, not as a valid C program.

Make the parameter environments explicit: each call has its own c, but every x refers to the same external p. The nonbase frames correspond to c after decrement 4, 3, 2, 1. Each increments p once, so p becomes 9 at the base return. A frame can fetch its multiplication operand before recursion, preserving its current 6, 7, 8or9, or fetch after recursion, seeing9. Thus a single deterministic answer requires an evaluation-order convention absent from the prompt. The official MTA must remain an adjudication label, not be replaced by whichever value one compiler happens to print.

Original paper, page 10 · Answer key