Python Recursive Function Example

Python Recursive Function Example

in the previous tutorial, we introduced you to functions in Python. In which post, we studied Python Recursion Function. and you will learn everything about recursive function in python with simple example.

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))

Output

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

  1. Recursive functions make the code look clean and elegant.
  2. A complex task can be broken down into simpler sub-problems using recursion.
  3. Sequence generation is easier with recursion than using some nested iteration.

Disadvantages of Recursion

  1. Sometimes the logic behind recursion is hard to follow through.
  2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
  3. 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.

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *