retagged by
24,209 views
39 votes
39 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
31 votes
31 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

24.3k
views
2 answers
37 votes
Arjun asked Feb 12, 2020
24,260 views
Consider the following C program.#include <stdio.h> int main () { int a[4] [5] = {{1, 2, 3, 4, 5}, {6, 7,8, 9, 10}, ... printf( %d\n , *(*(a+**a+2)+3)); return(0); }The output of the program is _______.
17.3k
views
8 answers
28 votes
Arjun asked Feb 12, 2020
17,253 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 ______.
17.0k
views
4 answers
25 votes
Arjun asked Feb 12, 2020
17,020 views
Consider a non-pipelined processor operating at $2.5$ GHz. It takes $5$ clock cycles to complete an instruction. You are going to make a $5$- ... processor over the non-pipelined processor (round off to $2$ decimal places) is_____________.
29.4k
views
6 answers
26 votes
Arjun asked Feb 12, 2020
29,382 views
A processor has $64$ registers and uses $16$-bit instruction format. It has two types of instructions: I-type and R-type. Each I-type instruction contains ... I-type opcodes, then the maximum number of distinct R-type opcodes is _______.