Saturday, January 22, 2022

How to call a Function recursively in Python Program

What is Recursive Function in Python Program
The function that calls itself one or more times inside of its definition body is known as recursive function. Generally, it is returning a return value of that function call.
The recursive function has to fulfill an essential condition to be used in a program that it has to terminate(not a infinite loop) at some stage.

Base Case:
A recursive function terminates, if with every recursive call, the solution of the problem has to be downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls.

Example 1:
The Factorial of any number is the product of all the integers from 1 to that number. 
The factorial of 5 (denoted as 5!) is 5*4*3*2*1 = 120.

def funFactorial(n):
    """function to find factorial of a number"""
    if n == 1:
        # Base Case
        result = 1
    else:
        # Recursive Case
        result = n*(funFactorial(n-1))
    return result

# calling the function
n = 5
print('The factorial of {} is: {}'.format(n, funFactorial(n)))

# Result:
The factorial of 5 is: 120

Notes:
When we call this function with the positive integer, it will recursively call itself by decreasing a number.
In each function call it multiples the number with the factorial of (number-1) until the number is equal to one.

When we are working with recursion, we should define the base case for which we already know an answer. In the above example, we are finding the factorial of the integer number, and we already know that factorial of 1 is 1, which is our base case.

Each successive recursive call to a function should bring it closer to the base case, which is precisely what we are doing in the above example.

# ------------------------------------------------------------------------------------------------------------------------ #

Example 2:
The program to print the numbers counting down from given number to 0 and then counting up from 0 to the given number using the recursive method in the function.

def countDownAndUp(number):
    print(number)
    if number == 0:
        # Base Case
        print('Count down ended, next Count up started')
    else:
        # Recursive Case
        countDownAndUp(number - 1)
        print(number)

# calling the function
countDownAndUp(10)

# Result:
10
9
8
7
6
5
4
3
2
1
0
Count down ended, next Count up started
1
2
3
4
5
6
7
8
9
10

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog