1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Programming Paradigms

Exercise

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.

Instructions 1/2

undefined XP
    1
    2
  • Fill out the condition for the base case.
  • Set the return value in the base case.