Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Recursion in programming is a very powerful tool, this is done with functions that call themselves over and over again. You can picture it as a kind of loop that iterates until a condition is met. Next we will see a brief example where we will use a recursive function to look for the maximum value in a list of numbers.
In this example we make use of a recursive function to find the largest number in a list, in this code we first make use of a conditional structure
if else to check if the length of the list is equal to 1, if so, we return the only number in the list. This would be the exit or base case, otherwise, we make a recursive call to the function
find_maximum_value() and we transfer as parameter the list minus the first value, to trim the list in each recursive call and finally we return the maximum value between the first number of the original list and the result of the function.
The recursivity or recursion is a concept that comes from mathematics and that applies to the world of programming. It allows us to solve problems or tasks where they can be divided into sub-tasks whose functionality is the same. Since the subproblems to be solved are of the same nature, the same function can be used to solve them. In other words, a recursive function is one that is defined as a function of itself, so it repeatedly calls itself until it reaches an exit point.
You will better understand how recursion works with the help of the following video.
The base case is fundamental when working with recursion since it defines when the process of recursive calls should stop. If you do not define a suitable base case, the function could execute indefinitely, draining your computer's resources and causing a system error.
Placing an incorrect base case is a very common mistake for someone starting to work with recursion, so remember that when creating a recursive function, the first thing to do before creating the function logic, is to create the base case and make sure it is well defined to prevent the function from calling itself indefinitely.
|Base case||It will allow us to end the recursive call cycle of the function at some point and not enter an infinite call stack.|
|Recursive case||In the recursive case we call the function over and over again in a stack of recursive calls, but we will get closer to the output solution (the base case).|
The following are some examples of recursion algorithms in Python.
Factorial calculus is a mathematical concept that involves multiplying a series of consecutive positive integers, starting from 1 up to a number n. It is recognized by the symbol "!", and is placed after the number. Example:
Example of factorial calculation using recursion with Python.
In this example, we make use of recursion to find the factorial result of a number n. First as in any recursion function, we create the base case with the help of a conditional
if else, if the number n is equal to 0 or is equal to 1 then we return the number 1 and break the recursion loop, otherwise, we call the function
factorial() and pass the number (n - 1) to find the factorial of all numbers less than n recursively and return the result of the function multiplied by n.
The Fibonacci series is a numerical sequence in which each number is the sum of the two previous numbers, the sequence starts with 0 and 1, then each following number is the sum of the two previous numbers, example:
Example of a fibonacci series using recursion.
In this example we make use of recursion to find all the numbers of the fibonacci series less than 1000. In this case, the base case is if the sum of the last two numbers in the list is greater than our limit number n; if so, we return the array and finish the stack of recursive calls, otherwise we add the sum of the last two numbers to the list and make the recursive call with the modified list again and again until the base case condition is met.
The sum of natural numbers refers to the addition of all positive integers from 1 to a number n. In mathematical terms, it is represented as the sum of a finite sequence of natural numbers. Example:
Example of addition of natural numbers using a recursive function.
In this example, the base case is if the number entered by parameter is equal to 0, if so, then we return 0 and end the stack of recursive calls, otherwise, we call the function
sum_natural_numbers() recursively and we pass as a parameter the number
(n - 1), finally we return the result of the function plus the number n this will give us the sum of all the numbers from n to 1.
Recursion is a powerful tool in programming that allows us to solve problems by breaking them into smaller subproblems. By correctly understanding the base case and the recursive case, we can create efficient recursive algorithms. Recursion is a complex programming topic that requires practice to fully understand it, I recommend that you practice as much as you can doing examples like the ones seen in this article. You can check the 4Geeks Blog to learn more interesting content.
Have fun creating recursion algorithms for your own applications! 😉