Python language Recursive function
- Home
- Function in Python
- Python language Recursive function
- On
- By
- 0 Comment
- Categories: Function in Python, function/method
Python language Recursive function
Python language Recursive function
In this tutorial, we discuss Python language Recursive function.
When a function calls itself it is called as recursion. The python language allows creating the recursive function to perform a special task
The following example explains how to find the adding of the limited natural numbers using the recursive function
Example of recursive function
Calculate the sum of natural numbers using recursion
The following is an example of the recursive function to total a series of the integer numbers
Program 1
def sum_Num(num): if num == 0: return 0 else: return num + sum_Num(num-1) print("Sum of the first 100 numbers is",sum_Num(100)) print("Sum of first 500 numbers is",sum_Num(500))
When the above code executed, it produces the following results
Sum of the first 100 numbers is 5050 Sum of first 500 numbers is 125250
In the above example, to find the total of given numbers, when we passing the argument of the positive integer of this function, it will recursively call itself decreasing one by one until it becomes one from a given number.
Program 2
following example explain how to find the factorial of a number using a recursive function in python.
factorial of a number is the product of the all integer from one to until the given number
For example, the factorial of 5 is 1*2*3*4*5n=120
Find factorial using recursion
#This is a example for find factorial #using recursive function in Pyton def calc_fact(n): if n==1: return 1; else: return(n*calc_fact(n-1)) num= int(input("Enter the number for find factorial: ")) # take input from user for find factorial print("The factorial of ",num," is", calc_fact(num)) #display factorial
When the above code executed, it produces the following results
Enter the number for find factorial: 5 The factorial of 5 is 120
In the above example, calc_fact() is a recursive function as it calls itself to find factorial.
Factorial of n is the product of all the integer from 1 to the factorial of n is 1*2*3*4……..*n-1*n.
In the above example, to find the factorial of a given number, when we pass the argument as the positive integer of this function, it will recursively call itself decreasing one by one until it becomes one from a given number
Advantages of recursion in Python
- Easy to understand and code readability and reduce the line of the program.
- A big and complex task easy to make using subprogram using recursion
- very flexible and repeatedly functioning is easier with using nesting iteration
Disadvantages of recursion in Python
- Tracing and debugging are very difficult
- every recursive allocates separate memory location so requires extra memory and very slow
Suggested for you
Find factorial using recursion in Python
Find factorial using recursion in C language
Find factorial using recursion in C++ language