notes blog about

To understand recursion, you must first understand recursion.

Every recursive function should have two parts:

Wrong (no base case -> infinite loop):

def countdown(i):
    print(i)
    countdown(i-1)

Right:

def countdown(i):
    print(i)
    if i <= 0:    # base case
        return
    else:         # recursive case
        countdown(i-1)

The call stack with recursions (from “Grokking Algorithms”)

def fact(x):
    if x == 1:
        return 1
    else:
        return x * fact(x-1)