Building a recursive function
You have seen an example of an iterative function to calculate a factorial that could also be defined recursively. Now is your chance to demonstrate your knowledge of recursive functions by implementing the recursive version of that function!
As a reminder: the "factorial" function, usually denoted with a !
, is defined as the product of all positive integers from 1 to the input value. For some examples:
- 1! = 1
- 2! = 1 * 2 = 2
- 3! = 1 * 2 * 3 = 6 … and so on.
Keep in mind that 0!
is defined to be 1
.
In the first step, you'll need to implement the "base case" for factorial by returning the value of n!
for the smallest value where factorial is defined. In the next step, you'll need to implement the logic to call the recursive function inside itself, recursively.
Cet exercice fait partie du cours
Programming Paradigm Concepts
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
def recursive_factorial(n):
# Base case -- check base condition
if ____:
# Return appropriate value
return ____