🧩Recursion
Also known as Recursive Function
A function that calls itself on a smaller job. A stop rule ends the chain.
Think of it like
Think of it like nesting dolls. Open one, find a smaller one, and go on to the tiny one.
Example
Why it matters
Some problems are built from smaller copies of themselves, like folders in folders. Recursion fits those.
Definition
Example code
python
def countdown(n):
if n == 0:
print("Go!")
return
print(n)
countdown(n - 1)
countdown(3) # 3, 2, 1, Go!Common mistake
Key takeaways
- 1Calls itself. The function uses itself on a smaller part.
- 2Needs a base case. A stop rule ends the chain.
- 3Answers flow back. Each call hands its result up.