GATE 2013 COMPUTER SCIENCE & INFORMATION TECH. – CS – C Programming | Q. 42

📌 GATE 2013 – Question 42
Q.42 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;
}
(A) 3024     (B) 6561     (C) 55440     (D) 161051
📌 Given Function
int f(int &x, int c) {
    c = c - 1;
    if (c == 0) return 1;
    x = x + 1;
    return f(x, c) * x;
}
☎️ Function Call
int p = 5;
f(p, p);
🔎 How are the two parameters passed?
• The first parameter x is passed by reference using &x. Therefore, changes to x also change the original variable p.
• The second parameter c is passed by value. Therefore, c receives a separate copy of the value.
🚀 Initial Values
Since p = 5 before the function call:
x → p = 5   (reference)
c → 5   (copy)
💡 Remember: x refers to the original p, whereas c is only a copy. This difference is important when tracing the recursive calls.
🔄 Call 1: f(x, 5)
c = 5 - 1
c = 4
🔎 Check: Is c = 0? No.
x = x + 1
x = 6
💡 Since x refers to p (passed by reference), changing x also changes p.
p = 6
➡️ Now:
return f(x, 4) * x;
📌 After Call 1: p = 6, c = 4, and the next recursive call is f(x, 4).
🔄 Call 2: f(x, 4)
📍 Current value:
p = 6
c = 3;
x = x + 1;
💡 Here, c is reduced from 4 to 3. Since x is passed by reference, increasing x also increases the original variable p.
➡️ So:
p = 7
↩️ Returns:
f(x, 3) * x
📌 After Call 2: p = 7, c = 3, and the next recursive call is f(x, 3).
🔄 Call 3: f(x, 3)
c = 2;
x = x + 1;
💡 c decreases from 3 to 2. The reference parameter x increases by 1.
p = 8
↩️ Returns:
f(x, 2) * x
📌 After Call 3: p = 8, c = 2, and the next recursive call is f(x, 2).
🔄 Call 4: f(x, 2)
c = 1;
x = x + 1;
💡 c decreases from 2 to 1. Since x is passed by reference, increasing x also changes p.
p = 9
↩️ Returns:
f(x, 1) * x
📌 After Call 4: p = 9, c = 1, and the next recursive call is f(x, 1).
🔄 Call 5: f(x, 1)
c = 0;
💡 Therefore: the condition c == 0 is true, so the function immediately executes return 1;.
return 1;
🔙 Unwinding the Recursion
At this point, the recursive calls have reached the base case. The function has returned 1, so now the previous calls begin to return in reverse order.
p = 9
💡 Remember: x is a reference to p. Therefore, after the recursive descent, x = 9 and p = 9.
🔙 Unwinding the Recursion
💡 The base case returned 1. Now the recursive calls return one by one in the reverse order. At this stage, x = 9.
↩️ Call 4 returns
1 × 9 = 9
↩️ Call 3 returns
9 × 9 = 81
↩️ Call 2 returns
81 × 9 = 729
↩️ Call 1 returns
729 × 9 = 6561
🎯 Final Answer
✅ (B) 6561

Call by Value and Call by Reference

A simple way to understand these two concepts is to imagine a classroom and a student’s marks record.

🟢 Call by Value – Give a Copy

Imagine a teacher gives a student a photocopy of the marks record. The student changes the photocopy from 80 to 100.

The teacher’s original record is still 80.

Simple rule:
“I give you a copy. You can change your copy, but my original does not change.”

🔵 Call by Reference – Access the Original

Now imagine that the teacher gives the student access to the original marks record. If the student changes the marks from 80 to 100, the original record also becomes 100.

Simple rule:
“I give you access to the original. If you change it, my original changes too.”

Quick Comparison

Call by Value Call by Reference
A copy is passed. Access to the original is provided.
Changing the copy does not change the original. Changing the original changes the caller’s value.
Remember:
🟢 Call by Value → Copy
🔵 Call by Reference → Original

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »