GATE CS 2020 Set 1 — Question 56
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.NAT+2 / -0MediumFunctions & RecursionC ProgrammingProgramming & Data StructuresStorage Classes
Programming & Data Structures → C Programming → Functions & Recursion
Last updated
Question
Consider the following C functions.The return value of fun2(5) is ________.
int fun1(int n) {
static int i = 0;
if (n > 0) {
++i;
fun1(n-1);
}
return(i);
}
int fun2(int n) {
static int i = 0;
if (n > 0) {
i = i + fun1(n);
fun2(n-1);
}
return(i);
}
Correct answer
55 to 55
Solution
The function
fun1(n) has a static variable i. When called with n, it increments i recursively times. Since i is static, it accumulates values across calls. Specifically, fun1(n) adds to the static i and returns the new value.The function fun2(n) also has a static variable i (let's call it j to avoid confusion). It calls fun1(n), adds the result to j, and recurses.Trace:1.
fun2(5) calls fun1(5). fun1 adds 5 to its static var (0+5=5). Returns 5. fun2 adds 5 to j (0+5=5).2.
fun2(4) calls fun1(4). fun1 adds 4 to its static var (5+4=9). Returns 9. fun2 adds 9 to j (5+9=14).3.
fun2(3) calls fun1(3). fun1 adds 3 to its static var (9+3=12). Returns 12. fun2 adds 12 to j (14+12=26).4.
fun2(2) calls fun1(2). fun1 adds 2 to its static var (12+2=14). Returns 14. fun2 adds 14 to j (26+14=40).5.
fun2(1) calls fun1(1). fun1 adds 1 to its static var (14+1=15). Returns 15. fun2 adds 15 to j (40+15=55).6.
Answer: 55fun2(0) returns j = 55.Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on C Programming
2026 Set 2 Q12The set T represents various traversals over binary tree. The set S represents the order of…2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q23Let be an odd number greater than 100. Consider a binary minheap with elements stored in an…2026 Set 1 Q24Consider a hash table that is initially empty. The hash table is maintained…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]