Log in from a computer to take this course

You'll need to log in from a computer to start Introduction to Algorithms and Linear Data Structures in Swift. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store
Learn

In the last exercise, you created a condition that determines whether or not findFactorialRecursively() calls itself. We call this if block the recursive case.

In recursion, the recursive case is the condition under which a function calls itself. We call this the recursive case because, recursion is defined as a process that includes a function calling itself.

At this point, we are still missing a couple of components from our recursive method:

  1. Calculating the product of each number. While the current implementation does access all the numbers that we need to multiply, we do not calculate their product.

  2. findFactorialRecursively() always returns 0 – the value set to recursiveSolution (see Factorial.swift to the right) is zero, because we always return 0 after the if block in findFactorialRecursively().

We’ll tackle #1 next.

Instructions

1.

Inside the if block, return the product of n and your call to findFactorialRecursively().

After you run your code, you should see that the value saved to findFactorialRecursively is still 0. Why didn’t it return the values from the product of n * findFactorialRecursively(n - 1)?

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?