Exams › GATE › Technical
What is the return value of f(p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f (int &x, int c) {
c = c - 1;
if (c == 0) return 1;
x = x + 1;
return f(x, c) * x;
}
- 3024
- 6561
- 55440
- 161051
Correct answer: 6561
Solution
x is passed by reference to p throughout, while c (by value) counts 5->4->3->2->1->0, incrementing the shared p four times from 5 to 9. The base case returns 1, and each of the four returns multiplies by the final shared value 9, giving 9^4 = 6561.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →