Posts

Showing posts with the label Recursion

Python3 Recursive Function

Python Recursion  Recursion :      We all know in Python, a function can call other functions. It is even possible to call itself. These type of construct are termed as recursive functions. Example        def factorial(num):              return 1 if num==1 else (num*factorial (num-1))        num=5        print("factorial of {0} is {1}".format(num, factorial(num))) output : factorial of 5 is 120 Advantages    1. Recursive functions make 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 some nested iteration. Disadvantages   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.     Python program to display the fibonacci sequence up to n-th term using recursive function        def fibonacci(num):              return