Recursion
A recursive function solves a problem by calling itself on a smaller version of the same problem. Every recursive function needs two parts: a base case that stops (the smallest problem, answered directly) and a recursive case that makes the problem smaller and calls itself.
Factorial is the textbook example: 5! = 5 * 4!, and 1! = 1 is the base case. Without a base case the function calls itself forever and Python raises a RecursionError.
Behind the scenes each call waits on the call stack until the call below it returns.
What stops a recursive function from running forever?
Write a recursive total(n) that returns 1 + 2 + ... + n. Print total(10).
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.