Programming FundamentalsNot Started
Recursion
Functions that call themselves to solve problems
0%
Knowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Recursion is when a function calls itself. Every recursive function needs:
- 1A base case — the condition that stops the recursion
- 2A recursive case — the problem broken into a smaller version
python
def factorial(n):
if n == 0: # base case
return 1
return n * factorial(n - 1) # recursive caseprint(factorial(5)) # 120 ```
Without a base case, recursion causes a stack overflow (Python raises RecursionError after ~1000 calls).
Examples
Fibonacci sequence
Classic recursion example
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
print(fib(8)) # 21Next in Programming Fundamentals
OOP Basics