Recursion

Recursive programming is a procedure in which a method calls itself, so that a problem is solved more and more with each method call. This continues until the problem is reduced to a very simple case.

Some tasks in computer science can be solved well by reducing a large problem step by step to smaller and smaller problems of the same kind until simple (trivial) solutions arise . From this finally the solution of the original large problem is put together.

Syntax

def function_name(parameters):
    # code to be executed
    return function_name(parameters)

Example: factorial

def factorial(number):
    if number < 2:
        return 1
    else:
        return number * factorial(number - 1)

print("factorial of 8 is " + repr(factorial(8)))
Output
factorial of 8 is 40320

Example: fibonacci

def fibonacciAlgorithm(number):
    if number == 0 or number == 1:
        return number
    else:
        return fibonacciAlgorithm(number - 1) + fibonacciAlgorithm(number - 2)

print("fibonacci number of the number 10 is " + repr(fibonacciAlgorithm(10)))
Output
fibonacci number of the number 10 is 55