Recursion

Applies to: general

Recursion is a function that calls itself on a smaller version of the problem, with a base case that stops the descent. Many problems (trees, divide-and-conquer, factorials) are naturally recursive.

def factorial(n):
    if n <= 1:        # base case
        return 1
    return n * factorial(n - 1)   # recursive case

See also: function, stack, loop