Previous

Functions

5 / 6
Next

Recursion is when a function calls itself to solve a smaller version of the same problem.

def print_all_numbers(n):
  print(n)
  print_all_numbers(n-1)

Programmers should always make sure that recursive functions have a base case. Otherwise, the function will never stop calling itself, and the Python program never terminates.

def print_all_numbers(n):
  print(n)
  if n > 0:
    print_all_numbers(n-1)
Send

Recursion

Hint
Beginner

The factorial $n! = n \cdot (n-1) \cdot \dots \cdot 2 \cdot 1$ can be described recursively: $$ n! = n \cdot (n-1)! $$ As an example, consider $$5 = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 = 5 \cdot 4!$$ Define the function fac that computes the factorial of a number n recursively.

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!