Recursion can be classified as direct recursion or indirect recursion. If a function makes a call to itself, then it is an example of direct recursion.
For example: in the factorial function, the find_factorial() function made a call to itself. Therefore, we can call it an example of direct recursion. However, sometimes there are scenarios where a function f() may call another function f1(), which in return makes a call back to function f(). This is known as Indirect Recursion. As an example, have a look at the following code:
def happy_new_year(n=1):
if(n<=0):
how_many_times()
for i in range(n):
print("Happy New Year")
def how_many_times():
val = input("How many times should I print?:")
if val=='':
happy_new_year()
else:
happy_new_year(int(val))
how_many_times()
In the preceding example, there is a function called happy_new_year() that prints the “Happy New Year” message as many number of times as you want. If no value is provided, then it takes the default value as 1, and prints the message once. If the value is invalid, that is, 0 or below, if calls a function how_many_times() which prompts the user again to provide another value and makes a call back to happy_new_year() with the new value. Have a look at the following figure:
Let’s have a look at some interesting output for this code:
Case 1: Call is made to happy_new_year() with no input value.
Output:
Happy New Year
Explanation: Since no value was provided the message got printed only once as that is the default value.
Case 2: Callis made to happy_new_year()for n=0.
Output: Since n =0, how_many_times()is called again till a value of n greater than 0 is provided.
How many times should I print?:0
How many times should I print?:0
How many times should I print?:7
Happy New Year
Happy New Year
Happy New Year
Happy New Year
Happy New Year
Happy New Year
Happy New Year
>>>
Explanation: The user provided n = 0 twice as a result of which the function happy_new_year() called how_many_times() back each time till the user decided to provide a value greater than 0 which in this case is 7. So, for n <=0, the function prompts the user to provide another value, and the function happy_new_year() is called again with the new input value.
Case 2: Call made with invalid values again and again.
Output:
happy_new_year(-2)
How many times should I print?:-3
How many times should I print?:-8
How many times should I print?:0
How many times should I print?:2
Happy New Year
Happy New Year
Explanation: An invalid value is passed on to happy_new_year() function, which then calls the how_many_times() function which prompts users to provide a valid number. However, the user continues to provide invalid values so both the functions keep on calling each other till a valid value is provided.
Finally let’s have a look at advantages and disadvantages of recursion:
Advantages of recursion
- Requires fewer lines of code. The code looks clean.
- Allows you to break a complex task into simpler tasks.
Disadvantages of recursion
- Forming the logic for recursion can sometimes be difficult.
- Debugging a recursive function can be difficult.
- Recursive functions consume more memory and time.
0 comments:
Post a Comment