Introduction

Hello, fellow explorer! Today, we will unravel the mystery of "Recursion" — a concept as enthralling as the patterns formed by two mirrors facing each other. Our aim is to decipher recursion, understand its inner workings, and master its application in programming.

Understanding Recursion

Consider a stack of books. Want the bottom one? You'll need to remove each book above it, one by one. It's a recurring action — an example of recursion. In programming, recursion involves a method calling itself repeatedly until a specific condition is met, similar to descending stairs one step at a time until you reach the ground.

Here's a simple Ruby function illustrating recursion:

Ruby
1def recursive_function(x) 2 if x <= 0 # Termination condition --> base case 3 puts "Base case reached" 4 else 5 print x 6 recursive_function(x - 1) # Recursive function call --> recursive case 7 end 8end 9 10recursive_function(5) 11# Output: 12# 54321Base case reached

This function keeps calling itself with x getting lower by one until x <= 0, which is our base case. At this point, it stops the recursion.

Defining the Base Case

The base case acts like a friendly signpost, telling the recursion when to stop. In our book stack example, reaching a point where no more books are left to remove serves as the signal. Similarly, x <= 0 is our base case in our function. The base case is crucial as it prevents infinite recursion and related errors.

Defining the Recursive Case

The recursive case is an essential part of recursion — the rule responsible for creating smaller versions of the original problem. Each call brings us a step closer to the base case. Let's use the process of calculating a factorial as an illustrative example.

To find a factorial, we multiply a number by the factorial of the number minus one and repeat this process until we get to one (our base case):

Ruby
1def factorial(n) 2 if n == 1 || n == 0 # base case 3 return 1 4 else 5 return n * factorial(n - 1) # recursive case 6 end 7end 8 9puts factorial(3) # we expect 6 (3 * 2 * 1)

In this case, when we call factorial(3), it returns 3 * factorial(2), where factorial(2) returns 2 * factorial(1). As factorial(1) is a base case, it returns 1. As a result, the whole recursion chain returns 3 * 2 * 1.

Tips for Thinking Recursively

To think recursively, visualize the problem like an onion. Peeling each layer brings you closer to the center. The center of the onion represents the base case, and the peeling process denotes the recursive case.

Remember that a complex problem often contains smaller, simpler sub-problems. You can trust these sub-problems to be solved correctly, culminating in an elegant solution.

Another Example of Recursive Function

Let's develop a function that calculates the sum of an integer's digits. Normally, it would involve using a loop, but with recursion, it can be done more easily:

Ruby
1def sum_of_digits(num) 2 # Base case: if num is less than 10, return num itself 3 if num < 10 4 return num 5 else 6 return num % 10 + sum_of_digits(num / 10) # Recursive case 7 end 8end 9 10puts sum_of_digits(12345) # Will print out 15 (1+2+3+4+5)

In this example, we use the same principle as with factorial calculation, but we pass num / 10 to the next recursion call, chopping off the last digit every time.

Conclusion and Lesson Summary

Let's pause here. We've unveiled the concept of recursion, determined the roles of base and recursive cases, and written a simple recursive function in Ruby. It's time to unlock its full potential through continuous practice, building a solid foundation for the upcoming sorting and searching algorithms lessons. Remember — practice illuminates knowledge. Happy experimenting!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal