Anatomy of Recursion
Recursive functions have key parts. Click each highlighted piece of the `countdown` function below to identify it.
def countdown(n):
if n <= 0:
print("Blastoff!")
else:
print(n)
countdown(n - 1)
Identify all 5 key parts!
Spot the Base Case
A recursive function *must* have a base case to stop. Click only on the line(s) that represent a correct base case condition or action. Then click "Check Selection".
Recursive Code Simulator
Try writing a recursive `countdown(n)` function like the one in the Anatomy section. Make it print numbers from `n` down to 1, then "Blastoff!". Test it by calling `countdown(3)`.
Recursive Mini Missions!
Apply your recursion knowledge! Write the functions described below and test them.
Mission 1: Recursive Countdown
Write a recursive function `countdown(n)` that prints numbers from `n` down to 1, then "Done!". Test it by calling `countdown(4)`.
Mission 2: Recursive Factorial
Write a recursive function `factorial(n)` that *returns* the factorial of n (n!). Remember, 0! = 1. Then print the result of `factorial(5)`.
Mission 3: Iterative Factorial (Challenge)
Now, write an *iterative* (using a loop) function `iterative_factorial(n)` that also returns n!. Print the result of `iterative_factorial(5)`.
Module 30 Practice Complete!
Great job exploring recursion! It's a powerful concept.
Ready for more? Try the Advanced Practice for Module 30.