A function that calls itself is called a recursive function. A recursive function must definitely have a condition that exits from calling the function again. hence there must be a condition that calls the function itself if that condition is true.if the condition is false then it will exit from the loop of calling itself again.
def countdown(n): if n<=0: print 'Blastoff!' else: print n countdown(n-1)
If n is 0 or negative, it outputs the word, “Blastoff!” Otherwise, it outputs n and then calls a function named countdown-itself-passing n-1 as an argument.
1.Example for recursion:
def factorial(n): print("factorial has been called with n = " + str(n)) if n == 1: return 1 else: res = n * factorial(n-1) print("intermediate result for ", n, " * factorial(" ,n-1, "): ",res) return res print(factorial(5)) OUTPUT: factorial has been called with n = 5 factorial has been called with n = 4 factorial has been called with n = 3 factorial has been called with n = 2 factorial has been called with n = 1 intermediate result for 2 * factorial( 1 ): 2 intermediate result for 3 * factorial( 2 ): 6 intermediate result for 4 * factorial( 3 ): 24 intermediate result for 5 * factorial( 4 ): 120 120