Need help understanding a recursion example in Python

Posted by Ali Mustafa on Programmers See other posts from Programmers or by Ali Mustafa
Published on 2014-07-26T07:08:29Z Indexed on 2014/08/25 10:32 UTC
Read the original article Hit count: 370

Filed under:

Python is my first programming language, and I'm learning it from "How to Think Like a Computer Scientist". In Chapter 5 the author gives the following example on recursion:

def factorial(n): 
  if n == 0: 
    return 1 
  else: 
    recurse = factorial(n-1) 
    result = n * recurse 
    return result 

I understand that if n = 3, then the function will execute the second branch. But what I don't understand is what happens when the function enters the second branch. Can someone explain it to me?

© Programmers or respective owner

Related posts about python