Your base case is return 0
. After the line return n * bounce(n - 1)
where n is 1, bounce(0)
will be executed, returning 0 and multiplying all your previous results by 0.
Following the calls, we see:
- 5>=1, so return
5*bounce(4)
- 4>=1, so return
5*4*bounce(3)
- 3>=1, so return
5*4*3*bounce(2)
- 2>=1, so return
5*4*3*2*bounce(1)
- 1>=1, so return
5*4*3*2*1*bounce(0)
- 0<1, so return
5*4*3*2*1*0
meaning everything gets zeroed out at the end.
You want the base case to be 1 for that reason.
2
solved Why does this recursion return 0?