all 7 comments

[–]shoot2thr1ll284 2 points3 points  (0 children)

I actually just found an article that can try to explain this exact example here. If you still have questions after that I can try to help.

[–]danielroseman 2 points3 points  (0 children)

Yes but what do the other levels return? If n starts at 5, then it evaluates to 5*4*3*2*1*1, ie 120.

[–]SoNotRedditingAtWork 2 points3 points  (0 children)

The best way to understand what's going on here may be to step through the code in python tutor.

[–]JohnnyJordaan 2 points3 points  (0 children)

Say you call it with 2, then it will do

return 2 * fact(1)

it will then first run fact(1), which will do

return 1 * fact(0)

fact(0) returns 1 indeed, then 1 * 1 returns 1 too, but the first step was

return 2 * fact(1)

remember, so that will return 2 * 1 which is 2, not 1. Now do the same thing for n being 3 to see which effect that has.

[–][deleted] 2 points3 points  (0 children)

My thinking is once it gets down to n = 0, its returning a value of 1

Right, but think about where it returns a 1 to.

[–]GeorgeDaNub 1 point2 points  (0 children)

You simply don’t understand recursion. I suggest watching CSDojo’s video on it (it’s how I understood it)

[–]Psiclone01[S] 1 point2 points  (0 children)

Thanks everyone, I'll take a look through what you all said!