RECURSIVE FUNCTION IN PYTHON WHAT IS A RECURSIVE

  • Slides: 13
Download presentation
RECURSIVE FUNCTION IN PYTHON

RECURSIVE FUNCTION IN PYTHON

WHAT IS A RECURSIVE FUNCTION ? When a function calls itself it is known

WHAT IS A RECURSIVE FUNCTION ? When a function calls itself it is known as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion. Here is how recursion works. A recursive function calls itself. As you’d imagine such a process would repeat indefinitely if not stopped by some condition. This condition is known as base condition. A base condition is must in every recursive programs otherwise it will continue to execute forever like an infinite loop.

RECURSIVE FUNCTION Factorial is denoted by number followed by ( ! ) sign i.

RECURSIVE FUNCTION Factorial is denoted by number followed by ( ! ) sign i. e 4! For e. g 4! = 4 * 3 * 2 * 1 2! = 2 * 1 0! = 1

RECURSIVE FUNCTION def fact(n): if n == 0: return 1 else: return n *

RECURSIVE FUNCTION def fact(n): if n == 0: return 1 else: return n * fact(n-1) print(fact(0)) print(fact(5)) Expected Output:

fact(5) 5*24=120 fact(5) # 1 st call with 5 5 * fact(4) # 2

fact(5) 5*24=120 fact(5) # 1 st call with 5 5 * fact(4) # 2 nd call with 4 fact(4) 4*6 =24 5 * 4 * fact(3) # 3 rd call with 3 5 * 4 * 3 * fact(2) # 4 th call with 2 5 * 4 * 3 * 2 * fact(1) # 5 th call with 1 5 * 4 * 3 * 2 * 1 # return from 5 th call as number=1 5 * 4 * 3 * 2 # return from 4 th call a 5 * 4 * 6 # return from 3 rd call 5 * 24 # return from 2 nd call 120 # return from 1 st call fact(3) 3*2 =6 fact(2) 2*1 =2 fact(1) =1

RECURSIVE FUNCTION Now try to execute the above function like this print(fact(2000)) You will

RECURSIVE FUNCTION Now try to execute the above function like this print(fact(2000)) You will get Runtime. Error: maximum recursion depth exceeded in comparison This happens because python stops calling recursive function after 1000 calls by default. To change this behavior you need to amend the code as follows.

RECURSIVE FUNCTION import sys. setrecursionlimit(3000) def fact(n): if n == 0: return 1 else:

RECURSIVE FUNCTION import sys. setrecursionlimit(3000) def fact(n): if n == 0: return 1 else: return n * fact(n-1) print(fact(2000))

ADVANTAGES OF RECURSION Recursive functions make the code look clean and elegant. A complex

ADVANTAGES OF RECURSION Recursive functions make the code look clean and elegant. A complex task can be broken down into simpler sub-problems using recursion. Sequence generation is easier with recursion than using some nested iteration.

DISADVANTAGES OF RECURSION Sometimes the logic behind recursion is hard to follow through. Recursive

DISADVANTAGES OF RECURSION Sometimes the logic behind recursion is hard to follow through. Recursive calls are expensive (inefficient) as they take up a lot of memory and time. Recursive functions are hard to debug.

FIBONACCI USING RECURSIVE FUNCTION def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n

FIBONACCI USING RECURSIVE FUNCTION def recur_fibo(n): """Recursive function to print Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) nterms = 10 if nterms <= 0: print("Plese enter a positive integer") else: print("Fibonacci sequence: ") for i in range(nterms): print(recur_fibo(i))

PRIME NUMBER IN A RANGE - RECURSIVE # Python program to display all the

PRIME NUMBER IN A RANGE - RECURSIVE # Python program to display all the prime numbers within an interval # change the values of lower and upper for a different result lower = 900 upper = 1000 print("Prime numbers between", lower, "and", upper, "are: ") for num in range(lower, upper + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)