What is recursion?
Recursion is the process of defining something in terms of itself.
A physical world example is a place two equal mirrors confronting one another. Any article in the middle of them would be reflected recursively.
Python Recursive Function
In Python, we know that a function can call different functions. It is even feasible for the function to call itself. These kinds of build are named as recursive functions.
The following image shows the working of a recursive function called
Following is an example of a recursive function to find the factorial of an integer.
Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.
Example of a recursive function
def factorial(x): """This is a recursive function to find the factorial of an integer""" if x == 1: return 1 else: return (x * factorial(x-1)) num = 3 print("The factorial of", num, "is", factorial(num))
The factorial of 3 is 6
In the above example,
factorial() is a recursive function as it calls itself.
When we call this function with a positive integer, it will recursively call itself by diminishing the number.
Each capacity duplicates the number with the factorial of the number beneath it until it is equivalent to one. This recursive call can be explained in the following steps.
factorial(3) # 1st call with 3 3 * factorial(2) # 2nd call with 2 3 * 2 * factorial(1) # 3rd call with 1 3 * 2 * 1 # return from 3rd call as number=1 3 * 2 # return from 2nd call 6 # return from 1st call
Let’s look at an image that shows a step-by-step process of what is going on:
Our recursion closes when the number reduces to 1. This is known as the base condition.
Each recursive capacity must have a base condition that stops the recursion or, in all likelihood the capacity calls itself unendingly.
The Python mediator restricts the profundities of recursion to help maintain a strategic distance from limitless recursions, bringing about stack floods.
By default, the maximum depth of recursion is 1000. If the limit is crossed, it results in
RecursionError. Let’s look at one such condition.
def recursor(): recursor() recursor()
Traceback (most recent call last): File "<string>", line 3, in <module> File "<string>", line 2, in a File "<string>", line 2, in a File "<string>", line 2, in a [Previous line repeated 996 more times] RecursionError: maximum recursion depth exceeded
Advantages of Recursion
1. Recursive functions make the code look clean and elegant.
2. A complex task can be broken down into a simpler sub-issues using recursion.
3. Sequence generation is simpler with recursion than using some nested iteration.
Disadvantages of Recursion
1. Some of the time the logic behind recursion is difficult to finish.
2. Recursive calls are costly (wasteful) as they take up a lot of memory and time.
3. Recursive functions are difficult to debug.
Please feel free to give your comment if you face any difficulty here.
For More Latest Articles Click on Below Link