+ 1
The output is 300...how?
def f(n): If n==0: return 0 else return f(n-1)+100 print(f(3))
3 Answers
+ 6
f(0) = 0
f(0) + 100 = 0 + 100 = 100
f(1) + 100 = 100 + 100 = 200
f(2) + 100 = 200 + 100 = 300
+ 4
When you run it for 3, it returns f(2) + 100. Then the f(2) runs, returning f(1) +100. Then f(1) runs, returning f(0) + 100. Then f(0) runs returning 0. So f(3) includes f(2) and hence f(1) and f(0).
Since f(0) = 0,
f(1) = 0 + 100 = 100;
f(2) = 100 + 100 = 200;
f(3) = 200 + 100 = 300;
+ 2
Chetna Gupta for n=3, it is 100
n=2, it is 200
n=1, it is 300