In this article you will learn to create a recursive function in python ....
Q. What is meant by recursive function ?
Definition of Recursive function in Python - A function that calls itself to solve a smaller version of its task until a final call is made .Recursion uses a top down approach to solve a problem .
Every recursive function has two major cases , which are as follows
1.BASE CASE
2.RECURSIVE CASE
Recursion used divide and conquer technique of problem solving Divide and
conquer technique is a method of solving a given problem by dividing it into smaller
instances each of these instances are solved recursively ,and solutions are combined
to produce a solution for the original problem .
|
Every recursive function have at least one base case.otherwise the recursive function will generate an infinite sequence of calls
|
Recursive function example :
To understand recursion ,let us take an example of calculating FACTORIAL of a number.
i.e. " n! "
We know that n! is nothing but n * (n-1)! .
so we will use this expression to write a program for factorial of number in python
CODE _
def fact(n):
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
n = int(input("Enter a Number"))
print("Factorial of a number",fact(n))
Display OUTPUT _
Enter a Number6
Factorial of a number 720
Process finished with exit code 0
From the above example ,there some basic steps should be analyze to understand recursion
1.Specify the base case which will stop the function from making a call ti itself .
2.Divide the problem into small parts.
3.call the function on sub problems.
4.Combine the result of the sub problem .
5.Return the result of the entire problem.
NOTE
|
The base case will work as a terminating condition ,so in the absence of an explicitly defined base case ,a recursive function call itself infinitely .
|
2.The Fibonacci series :
1 1 2 3 5 8 13 21 ...…
The third term of the series is the sum of the first and second , On similar grounds , fourth term is the sum of second and third terms, and so on .Now we will discuss a program for Fibonacci series
using and without using recursive functions.
CODE _
1.Write a program to calculate Fibonacci series ?
def fib(n):
a =0
b=1
if n==1:
print(0)
else:
print(a)
print(b)
c = a+b
a=b
b=c
print(c)
n=int(input("Enter a number"))
fib(n)
Display OUTPUT :
Enter a number7
0
1
1
2
3
5
8
CODE -
2.Write a program to find the Fibonacci series using a recursive function ?
-
def fib(n):
if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))
n = 5
if n<= 0:
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(n):
print(fib(i))
Display OUTPUT :
Fibonacci sequence:
0
1
1
2
3
In both ways we got the output ,but code required for a program in first case is much more as compared to code required in the second case .
SOME PROS AND CONS IN RECURSION:
PROS:
1.Code is smaller and simpler than non-recursive ones.
2.Code is clearer and easier to use .
3.It Follows a divide and conquer technique to solve a problem .
4.Recursion function makes the code look clean and elegant.
CONS:
1.For some concepts recursion looks difficult.
2.Its difficult to find bugs , when we use global variables .
I hope this article on recursion is helpful for u
Thank u😊...