This tutorial will discuss what recursive functions are and how to create and why we need them in python. So let's discuss Python Recursion.
What are recursive functions?
Until now, we were defining a function, and we were calling it from outside the function at the time of execution. What if we call the function inside the function? It will create a kind of loop of what recursion functions are all about. A recursive function is a function that can call itself during execution. For example, when a function body has a statement in which it calls itself, that function will be termed a recursive function. We have said that recursion acts like loops that can perform repetitive work, so there should be a base condition that stops that repetitive statement and give a result. You will get trapped in the infinite loop.
Python Recursive Function
There are many examples in which a function calls another function, but those are not recursive functions only those functions are recursive functions that can call themselves. Finding the factorial of a number is one of the classic examples of recursive functions. If you do not know what a factorial is, let’s understand it first. Factorial is a Math concept denoted by an exclamation mark (!). If you want the factorial of 5, you will write 5! And the solution of the factorial would be 5!= 5 x 4 x 3 x 2 x 1 = 120 And for note 1! =1 and 0! =1
Let’s code the factorial in Python
def fact(n): if n==1 or n==0: #Base condition return 1 else: return n * fact(n-1) # recursion function calling num=7 print("the factorial of" ,num, "is ", fact(num)) #function calling
the factorial of 7 is 5040
Behind the code
In the above example, to execute the function fact (), we call it from outside the function, then the value of variable num gets assigned to the function argument n, and the function gets started to execute when the argument n does not satisfy the if statement the else statement gets executed. In the else statement, we strike the recursive function, and let’s see how it works.
fact(n) #main function return 7 * fact(6) #recursive function first call return 7 * 6 *fact(5) #recursive function 2nd call (the value of fact(6) is 6 * fact(5)) return 7*6*5*fact(4) #recursive function 3rd call return 7*6*5*4 *fact(3) #recursive function 4th call return 7*6*5*4*3*fact(2) #recursive function 5th call return 7*6*5*4*3*2*fact(1) #recursive function 6th call return 7*6*5*4*3*2*1 #recursive function 7th call
At last, it returns a value of 7*6*5*4*3*2*1, and we get an output of 5040. Here you can see that the base condition plays a significant role what if we did not pass the base condition, the number kept on decreasing, and it could make an infinite recursive call.
Pro and Cons of recursive function:
- With the recursion function, the code looks neat
- It divides the functionality into smaller parts which make the program easy to understand
- It comes very usefully with the binary trees data structure
- It isn’t easy to understand the logic behind every recursive function.
- A recursive function is slow as compared to the iterators.
- It occupies a lot of memory because we are executing the same function again and again
- it is very hard to debug the recursive function.
- With a big set of data, recursive functions are not useful