Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Function Recursion


Function Recursion

Calling a function to itself is known as function recursion.

It is necessary to handle recursion carefully by the developer, when the recursive function's internal conditions are not handled properly, then the function never terminates and goes into an infinite loop of execution.

def decrement_number(number):
    if number <= 0:
        print("The number is zero.")
    else:
        print(number)
        decrement_number(number - 1)

decrement_number(5)
5
4
3
2
1
'The number is zero.'

In the above example, we called decrement_number recursively. When calling decrement_number recursively, we are decrementing the passed number counter value, and when the counter reaches zero, we end the recursion.