Recursion Functions in Python

Hunter
3 min readSep 20, 2021

--

One day, I was casually chatting with a friend of mine on Discord… We were talking about loops in Python and what can be used in place of a for loop…So he just told me about recursion functions… He showed me an example of a recursion function and guess what? I became a fan of them! They are just soo K00L to use… Sooo how do they even work???

Well recursive function is a type of function which calls itself continuously until a specific condition is reached… Didn’t understand much? No worries! Read ahead and I am sure you will understand them better!

Suppose I want to find the value of 2³⁸. There are two main ways to achieve it →
-> Using Loop
-> Using Recursion Function

The implementation using the first method, that is, using loops is given below!

For Loop →

def value(base, power):
answer = 1 #As the value can never be 0
for _ in range(power):
answer *= base
return answer

print(value(2, 2)) # Prints 4

While Loop →

def value(base, power):
answer = 1 #As the value can never be 0
while power != 0:
answer *= base
power -= 1
return answer
print(value(2, 2)) # Prints 4

Though the above methods are easier and are the methods which are taught to a beginner, there is a cleaner way to deal with this, using recursion!

Recursion Function →

def value(base, power):
if power == 0:
return 1
return base*value(2, power-1)
print(value(2, 2)) # Prints 4

So, this is how a recursion function work. It calls itself again and again until the first if statement is triggered. So what is happening here →

First Iteration →
base = 2
power = 2
The function returns 2*value(2, 1)

Second Iteration →
base = 2
power = 1
The function returns 2*2*value(2, 0)

Third Iteration →
base = 2
power = 0
As the power is 0, the first if statement is triggered and thus the function stops calling itself over and over again because it returns 1… So the value of the function no is 2*2*1 which is equal to 4!

I know it is a bit too much to take in (believe me, it took me a week to understand these properly), but once you get the hang of it, these can be a potential replacement for loops!

They are considered as a potential replacement of loops in some of the cases because of →
-> Low Line Usage
-> Better Readability
-> Easily Understandable by Anyone

Why the first if statement?

So that if statement is known as the base case. It tells the function when it should stop calling itself over and over again. You can think that it controls the whole function… Without it, a RecursionError would be raised because the function won’t know when to stop calling itself, thus Python kills the main thread before it eats up your RAM and Resources.

So, if we use the code I presented above and remove the base case, then it would result in the RecursionError, which can be seen in the pic below.

These are some functions which I think are fun to use if you understand how to use them correctly. Be sure to check them out and try to use them in your own projects!
I would love to see some more uses of recursion functions in the comment section :D

Contact me →

--

--

Hunter
Hunter

Written by Hunter

I am Hunter. Nice to meet you! I am a student and also a Python Coder. Stay away from me, I got a fetish for Python codes 😉

No responses yet