MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Programming Paradigm Concepts

Lihat Kursus

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

def recursive_factorial(n):
	# Base case -- check base condition
	if ____:
        # Return appropriate value
		return ____
Edit dan Jalankan Kode