The GATE Grind

GATE 2020 CS – Question 56

Programming and Data Structures · Recursion · 2 marks · Numerical answer

Consider the following C functions.

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);
}

The return value of fun2(5) is ______.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 55 to 55

Explanation

fun1's static i accumulates, so fun1(n) returns 5, 9, 12, 14, 15 for the successive calls with n = 5, 4, 3, 2, 1. fun2's static i sums these: 5+9+12+14+15 = 55.