Recursive functions are one of the hardest concepts you are going to learn. In reality, they can be dealt with a few lines of code, but all the problems that need recursion to be solved can be considered hard. In this post, I am going to show you how to run a simple recursive function in just a few lines of code.
Real use cases of recursive functions
When doing software engineering, there are problems that will require a process to run based on a value that will be updated at every iteration. Recursive functions are initially studied on pure math problems, like the Fibonacci sequence, but they can also be used for engineering purposes. Some practical examples of recursion are:
- Web Scraping
- Nested clustering
- Math simulations
- Debugging processes
During my career, I have encountered each one of these problems while working on different projects, and I dealt with them using recursive functions. Perhaps, you might not be able to build something very complex with them, in the beginning, but with time they become very useful tools to solve challenging engineering problems.
How to build a recursive function?
Before providing you with some valid code (this one pretty much is only used for a demonstration, is practically useless), I will show you what are the main elements that need to exist for a recursive function:
- Return call of the function itself
- An exit condition
def recursive(n): if n == 5: return n return recursive(n+1)+ recursive(0)
All functions return a value. In the case of a recursive function, we have to return the function again with a new value. If we just return the input value (n, instead of n+1), the function will loop infinitely.
After calling the recursive function, the value that we have passed in as initial input will keep changing. There is still a problem: we need to break the recursive function, soon or late, otherwise it will be running infinitely. To do this, we use an exit condition. When the input value will reach a value, we break the loop.
This is how the function will run: it will start from 0, and then will run until it reaches 5, for a total of 6 iterations.
Why should I use a recursive function?
The point of these code tricks, as you will learn while you progress in software engineering, is to make your code more efficient. Essentially, you can solve a massive problem that will require an endless amount of loops with a few lines of code. The main issue when implementing those functions is the transparency. Meaning, that the developer with whom you are going to share your work will need to perfectly understand what you have written.
This is a common issue when perfecting your code too much, it becomes more complex to understand rather than a step-by-step process.
In the next post, I am going to show you how to run different recursive functions, specifically the Fibonacci sequence and prime number checking. This is a sneak peek of the Fibonacci sequence using naive recursion, yet without any explanation:
def fibonacci(n): if n == 1: return 1 if n == 2: return 1 return fibonacci(n-1)+fibonacci(n-2) fibonacci(10)
In future posts, I will teach you how to merge this process with Machine Learning technology, like this Medium article I have been writing (Teaching AI the Fibonacci sequence).