Recursion

This is where a function calls itself. Let’s look at a classic example of recursion, the power of something. First we define the problem:

    power(x, 0) is 1 for all numbers x.
    power(x, n) for n > 0 is the product of x and power(x, n-1).


def power(x, n):
if n == 0:
return 1
else:
return x * power(x, n-1)

Tags: