Python Function Recursion
What is recursion in Python?
Recursion is the process of defining something in terms of itself.
or
Function calling itself is called recursion.
A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.
Python Recursive Function:
We know that in Python, a function can call other functions. It is even possible for the function to call itself. These type of construct are termed as recursive functions.
Following is an example of recursive function to find the factorial of an integer.
Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 (denoted as 6!) is 1*2*3*4*5*6 = 720.
Advantages of Recursion
1.Recursive functions make the 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 using some nested iteration.
Disadvantages of Recursion
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.
Example of recursive function
Question:1
Python script to calculate factorial of a no (Method:1)
Sol:
def fact(n): if(n==0): return(1) else: return(n*fact(n-1)) #function calling a=fact(4) print("Fact = ",a) a=fact(5) print("Fact = ",a) a=fact(0) print("Fact = ",a)
Output:
Fact = 24
Fact = 120
Fact = 1
>>>
Question:2
Python script to calculate factorial of a no (Method:2)
Sol:
def fact(n): if(n<0): return(-1) if(n==0): return(1) else: return(n*fact(n-1)) #function calling while True: a=int(input("Enter any no ")) b=fact(a) if(b==-1): print("-ve nos not allowed") else: print("Fact = ",b) ch=input("Like to cont ") if(ch=='y' or ch=='Y'): continue else: break
Output:
Enter any no 5
Fact = 120
Like to cont y
Enter any no 6
Fact = 720
Like to cont y
Enter any no 4
Fact = 24
Like to cont y
Enter any no 3
Fact = 6
Like to cont n
>>>
Question:3
Python script to calculate sum of first “n” numbers
Sol:
def sum(n): s=0 if(n==0): return(s) else: return(n+sum(n-1)) #function calling while True: a=int(input("Enter the limit ")) s=sum(a) print("Sum = ",s) ch=input("Like to cont .... (y/n) ") if(ch=='y' or ch=='Y'): continue else: break
Output:
Enter the limit 5
Sum = 15
Like to cont …. (y/n) y
Enter the limit 10
Sum = 55
Like to cont …. (y/n) y
Enter the limit 20
Sum = 210
Like to cont …. (y/n) n
>>>
Question:4
Python script to calculate product of first “n” numbers
Sol:
def prod(n): p=1 if(n==0): return(p) else: return(n*prod(n-1)) #function calling while True: a=int(input("Enter the limit ")) p=prod(a) print("Prod = ",p) ch=input("Like to cont .... (y/n) ") if(ch=='y' or ch=='Y'): continue else: break
Output:
Enter the limit 3
Prod = 6
Like to cont …. (y/n) y
Enter the limit 5
Prod = 120
Like to cont …. (y/n) y
Enter the limit 6
Prod = 720
Like to cont …. (y/n) y
Enter the limit 4
Prod = 24
Like to cont …. (y/n) n
>>>
Question:5
Python script to calculate sum of two non –ve numbers
Sol:
def sum_nos(x,y): #print(x,y) x=x-1 y=y+1 if(x==0): print("sum = ",y) else: sum_nos(x,y) while True: n1=int(input("Enter 1st no ")) n2=int(input("Enter 2nd no ")) if(n1<0 or n2<0): print("numbers should not be -ve") else: sum_nos(n1,n2) ch=input("Like to cont ... (y/n) ") if(ch=="y" or ch=="Y"): continue else: break
Output:
Enter 1st no 10
Enter 2nd no 20
sum = 30
Like to cont … (y/n) y
Enter 1st no 2
Enter 2nd no 6
sum = 8
Like to cont … (y/n) y
Enter 1st no 32
Enter 2nd no 6
sum = 38
Like to cont … (y/n) n
>>>