What is printed?
def f(n, acc=0):
if n == 0: return acc
return f(n - 1, acc + n)
print(f(4))
- 10
- 4
- 0
- 24
The accumulator sums 4+3+2+1 before the base case returns it.
Play Problem Solving ▸
def f(n, acc=0):
if n == 0: return acc
return f(n - 1, acc + n)
print(f(4))