Recursion

say it: “rih-KUR-zhun” /rɪˈkɜrʒən/

A function calling itself to solve a smaller piece of the same problem.

What is recursion?

Recursion is when a function calls itself to solve a smaller piece of the same problem. It needs a stopping point, or it runs forever.

def countdown(n):
    if n == 0:
        print("Liftoff!")
        return
    print(n)
    countdown(n - 1)

countdown(3)  # 3, 2, 1, Liftoff!

Level: Apprentice · More Python terms at this level

Updated

In other subjects