int f(int n){ static int i=1; if(n> : GATE 2004


int f(int n)
	static int i=1;
		return n;
	return f(n);

What is the value return by f(1) ?

(a) 5    (b) 6    (c) 7    (d) 8


Answer is (c) 7

As i is a static variable, its value will persist between different function calls. So, the value of i and n changes as follows when we call f(1):
n:    1    2    4    7
i :     1    2    3    4

Therefore, value return by f(1) is 7.

Vishakha @vishakha164
26 Jan 2016 06:54 pm

But the condition if(n>=5) is not satiesfied forf(1).

Ranita Biswas @ranita
30 Jan 2016 04:33 pm

Yes, that's why total four function calls are there. Execution of f(1) calls f(2), execution of f(2) calls f(4), and execution of f(4) calls f(7). Execution of f(7) finally satisfies (n >= 5) condition, and hence 7 is returned.