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

Find factorial using in Java

Find factorial using recursion in C language

Find factorial using recursion in C++ language

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago