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

  1. Easy to understand and code readability and reduce the line of the program.
  2. A big and complex task easy to make using subprogram using recursion
  3. very flexible and repeatedly functioning is easier with using nesting iteration

Disadvantages of recursion in Python

  1. Tracing and debugging are very difficult
  2. every recursive allocates separate memory location so requires extra memory and very slow

 

Suggested for you

Find factorial using recursion in Python

Python function

 

 

 

 

 

Python program to add two number using function
Calculate electricity power:Python program using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago