Summary - Recursion
- Algorithms course
- Beginner
- Lesson 40/80
- 4 min
Write a recursive counting function in a mini task.
Summary - Recursion
In this group you learned what recursion is, the importance of the base case and the recursive step. Recursion solves problems by dividing them.
Every recursive function needs a base case and a recursive step. Very deep recursion can cause stack overflow.
Practice Print (recursive)
Write print_recursive(n). Print numbers from n down to 1. Call print_recursive(3) to print 3, 2, 1.
Starter code
def print_recursive(n):
# Print numbersSample solution
def print_recursive(n):
# Print numbers
if n > 0:
print(n)
print_recursive(n - 1)
print_recursive(3)Expected output
3 2 1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Summary - Recursion: match the terms
| Term | Meaning |
|---|---|
Recursion |
Self-call |
Base case |
Stops |
Recursive step |
Reduces |
Stack |
Frame |
2 Recursion
Recursion is a function calling ____.
Recursion is a function calling itself.
Explanation: itself is right.
3 What stops recursion?
- For loop
- Base case Correct answer
- While loop
Explanation: The base case stops it.