Recursion in Python.


Recursion in Python:

When computation in a function is described in terms of functions itself, it is called a Recursive function. Pytyhon allows a function to call itself, possibly with new arguments.
Recursion aims to solve a problem by providing a solution in terms of the simpler version.
A recursive function definition comprises of two parts. The first part is the base part, which is the solution of the simplest version of the problem often termed stopping or termination criterion.
The second part is the inductive part, which is the recursive part which reduces the complexity of the problem in terms of a simpler version of the original problem itself.

let's try to understand recursion with the help of an example:

def factorial(n):
    if n==0 or n==1:
        return 1
    else:
        return n*factorial(n-1)
def main():
    n=int(input("Enter a number to calculate its factorial : "))
    result=factorial(n)
    print("Factorial of a number is :",result)
main()



Post a Comment

0 Comments