Python recursive function; Through this tutorial, i am going to show you what is recursive function in python and how to use it.
A recursive Function in Python
Here we will explain what is a recursive function in python, how to use the recursive function in python programs, how to call recursive function and much more about recursive function with examples.
What is recursive function in python
In any programming language like C, C ++, Java, PHP, and Python Eats. A function that calls itself is called a recursive function.
How to work recursive function
As we stated above the definition of a recursive function. As you imagine recursive means such a process will be repeated indefinitely if it does not stop in any situation. If this process is not stopped with the termination condition. The repeating programs then execute as infinite loops.
Note:- This prevents the execution of recursion function in the program. This is called the termination condition.
A recursive function work process:
- A recursive function is called by some external code.
- In program, if the test condition is met true then the program execution stop and exits.
- Otherwise, the function does some required processing and then calls itself to continuously (recursively).
Example of recursive function
The best way to explain the recursive function in Python is through a factorial program.
You will know how to factor out a number. And if you do not know, you can see the example below:
Like if you want to get the factor of number 4
4! = 4 * 3 * 2 * 1
2! = 2 * 1
0! = 1
python program to find factorial using recursive function
def findFactorial(x): """This is a recursive function to find the factorial of number""" if x == 1: return 1 else: return (x * findFactorial(x-1)) num = 5 print("The factorial of", num, "is", findFactorial(num))
The factorial of 5 is 120
Explanation in the above example
findFactorial () is a python recursive function and calls this function it itself.
When we call this recursive function with a positive integer, it will call itself and by subtracting the number again.
You can see the example given below. In this, we have explained how the factorial program in Python will be a recursive function of a recursive function.
findFactorial(5) # 1st call with 5 5 * 4 * findFactorial(4) # 2nd call with 4 5 * 4* 3 * findFactorial(3) # 3nd call with 3 5 * 4* 3 * findFactorial(2) # 4rd call with 2 5 * 4* 3 * 2 * findFactorial(1) # 5th call with 1 5 * 4* 3 * 2 * 1 # return from 5th call as number=1
Note:- Every recursive function must have a terminate condition to stop the execution of the program. Otherwise, the recursion function calls itself infinitely.
Advantages of Recursion
- Recursive functions make the code look clean and elegant.
- A complex task can be broken down into simpler sub-problems using recursion.
- Sequence generation is easier with recursion than using some nested iteration.
Disadvantages of Recursion
- Sometimes the logic behind recursion is hard to follow through.
- Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
- Recursive functions are hard to debug.
What is a base case/condition in recursive function
When working with a recursive function in python or another programming language, we should define the base case/condition to step the execution of recursive function. This condition stops calling the recursive function. It can be said that it goes out of the recursive loop.