Programming Methods: Recursion
In this article, we will cover a programming technique that is very complex and confusing in terms of implementation but very useful and simply brilliant.
What Is Recursion?
Recursion is a computer programming technique in which a function calls itself. If we have a recursive function A, then in recursion, we’d either have A call itself or it could call another function B that would then call A. It is either the recursive function directly calling itself or indirectly calling itself via another function like function B here. A simple recursive function looks as below:
def func()
|
|
func() //recursive call
Now that we have explained what recursion is, we’ll go through some examples to further our understanding.
Examples
Iterating without For Loop
This is a simple example to explain how we can use recursion. We are defining a function that takes an integer as an argument and increments it by one till it is equal to 10. Normally, we use a for loop for this but we are able to do this with just a recursive function as shown below:
def do_recursion(a):
a += 1
if a == 10:
return a
else:
return do_recursion(a)a = 1
print(do_recursion(a))
Of course, this example is not a use case by itself though easy to understand, but even if the condition had been something else, we could just do recursion until that condition was met. This is useful when we need to recursively check for a condition if it has become true yet or not. If you are waiting for a process that takes an indefinite time to complete in order to go to the next process, you can use a recursive call with if-else statements.
We’ll now go through a more complex example.
Factorial Function
We want to create a function that takes an integer input and returns its factorial. Before going through this solution, you can brainstorm the logic that will go behind this function. It’s rather complex using loops but easy and efficient using recursion.
The factorial function will be as below:
def recursive_factorial(n):
if n == 1:
return n
else:
return n * recursive_factorial(n-1)
We can see that we only need an if-else condition to make the recursion work. When n=1, we return n, otherwise we return n*function(n-1).
Since there can be a few edge cases as well depending on the input integer, the implementation of this is as below:
num = 10
# placing checks for edge casesif num < 0:
print("Invalid input! Please enter a positive number.")
elif num == 0:
print("Factorial of number 0 is 1")
else:
print("Factorial of number %s is %s", (num,
recursive_factorial(num)))
P.S: There are plenty of recursion memes. Do comment if you’ve got a good one.
Conclusion
Recursion, even if it is sometimes hard to follow, is very useful when we need to make a complex task easier. It is especially useful when your logic needs to go through multilevel iterations e.g. while traversing through nested lists or dictionaries. Here’s a detailed tutorial on recursion that properly demonstrates the difference between recursive and non-recursive approaches for some use cases.
Articles on Programming
Articles on Data Science and Machine Learning
About Ahmad Sachal
- LinkedIn Profile — linkedin.com/in/ahmad-sachal/
About Red Buffer
- Website — redbuffer.net
- LinkedIn Page — linkedin.com/company/red-buffer