retagged by
23,177 views
38 votes
38 votes

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 $\text{fun}2 (5)$ is _________

retagged by

3 Answers

Best answer
29 votes
29 votes

This illustration of function calling and values may help😀

$$\mathbf{Fig: Tracing\; by\; tree\; method}$$ Now $f1(5),$ if we observe it increments $ ‘i\text{’}, n$ times so $f1(5)$ will return $5.$

Similarly, $f1(4)$ will increment $ ‘i\text{’}$ $4$ times but $ ‘i\text{’}$ being static (just one memory location for entire program run instead of different memory location across function calls), it′ll resume from its previous value of $5.$  So, $f1(4)$ returns $9\;[5+4]$

By the same logic $f1(3)$ will return $12\; [9+3],$

$f1(2)$ will return $14\; [12+2],$

$f1(1)$ will return $15\; [14+1].$

$\therefore$ Return value $i=55$ and hence $55$ is the output of $f2(5).$

edited by
10 votes
10 votes

Answer is 55.it is calling static int , when calling fun1() next time, it will add with previous value.

 

6 votes
6 votes
Answer will be 55

As it is calling static int , when calling fun1() next time, it will add with previous value
Answer:

Related questions

28 votes
28 votes
8 answers
2
Arjun asked Feb 12, 2020
16,307 views
The number of permutations of the characters in LILAC so that no character appears in its original position, if the two L’s are indistinguishable, is ______.