PYTHON PROGRAMMING
Simply put, a recursive function is a function that calls itself.
This may sound simple, but if you try to learn more, you’re likely to find explanations of recursion that are not that simple. This is because the casual definition of recursion above doesn’t go into any details, and to fully comprehend how recursion works, you need to know more than what this simple sentence conveys:
A recursive function is a function that calls itself.
Check out Wikipedia for example. Full of technical jargon, the explanation is far from simple, especially for beginning programmers without IT- or math-related education. It’s difficult to imagine a beginner trying to implement their own recursive function based solely on such an explanation.
Even though some recursive functions may look quite simple at first glance, trying to implement your first recursive logic may occur very difficult. That can be a tough task because one needs to change one’s thinking about problems.
Most data scientists see programming tasks as a sequence of smaller steps that lead to a final solution. Since we’re used to iteration, we can feel confused by these two entirely distinct logics: iteration and recursion.
In this article, I want to demonstrate that recursion doesn’t have to be as intimidating as it may seem. It’s not meant to be a technical deep dive into recursion. Instead, I’ll focus on basic applications of recursion, highlighting its practical value in certain tasks. Understanding this logic can be beneficial — especially for data scientists, who need to work with various sorts of business and computational sorts of logic — and frankly, you can use recursion without knowing all of its technical intricacies.
We will analyze three simple recursive functions: one representing the so-called flat recursion pattern and two representing the nested recursion pattern. One of them will be probably the most often used recursion example, that is, calculating the factorial of a number. However, it is the other…