Recursion or Recursive Functions in Python Programming

Recursion in PythonRecursion or Recursive function both are the same terms. Recursive functions are those functions which call itself again and again until it reached to its base limit.
In the case of a recursive function, the maximum loop executes 1000 times. After that, it terminates automatically.

python-video-tutorials

In other programming languages, there are no limitations to the recursive function.

SyntaxSyntax of making function recursive has been given below:

#Syntax of recursion

def fncn_name():
    //statement
    fncn_name()   #function called inside function....

fncn()

#After executing loops to 1000 times, loop will terminate automatically

Example 1:

#Recursion Example

x = 0
def fncn():
    global x
    x = x+1
    print(x)
    fncn()

fncn()

Output:

# l
# 2
# 3
# ..
# ..
# ..
# 1000

Base case: The base case is the known term or value about which we are 100% confirm. Suppose we talk about factorial of a number ie. factorial of 1 is always 1. So we can say this as a base case. We use base case to stop the execution of loop when we found the result or output. If the base case is not used, then the loop keeps on executing.

Example 2Example to understand the use of base case:

#Recursion example in Python to Find the sum of N natural number

def add(n):
    if(n==1):
        return 1
    else:
        return (n+add(n-1))

n = 10
print("Sum of", n, "is: ", add(n))

Output:

# 55