Skip to main content

Python | Recursive Function

Python - Recursive Function

Recursive function means the function call themselves within the function body until a certain condition is matched then this function is recursive function.

For example:- 

def fact(n):
     if n==0 or n==1:
          return 1
     else:
          return n*fact(n-1)

print(fact(8))

Output:-
40320

This is the example of recursive function. In this program we create a function named as fact and we call this function within the same function.
How this program works?
We give the value of n is 8 so n=8. In the first if condition, 8 is not equal to 0 and 1 so it goes to the else part in the else part the sentence is return n*fact(n-1) that means 8*fact(7) now here value of n become 7 to find fact(7) again same procedure repeat up to fact(1).  Because fact(1)=1  we give this condition. Then 
fact(2) => 2*fact(1) => 2*1=> 2  
fact(3)=> 3*fact(2) => 3*2=> 6
fact(4)=>4*fact(3)=>4*6=>24
fact(5)=>5*fact(4)=>5*24=>120
fact(6)=>6*fact(6)=>6*120=>720
fact(7)=>7*fact(6)=>7*720=>5040
fact(8)=>8*fact(7)=>8*5040=>40320

All this process done in background to find factorial using recursive. You can write this program without using recursive function. Now we will see how to write this program without using recursive function.

num = 8
fact = 1
if num == 0 or num == 1:
     print("1")
else:
     for i in range(1,num+1):
          fact = fact*i
     print(fact)
     
Output:
40320

You can see the difference of both programs. Recursive function reduce unnecessary functions. Recursive function reduce the size of program. In recursive function you must have to write if condition because they call themselves until certain condition is matched. 

We will see another example of recursive function.
1) Fibonacci series 
Ans:-

def fib(n):
     if n == 0:
          return 0
     elif n == 1:
          return 1
     else:
          return fib(n-1)+fib(n-2)
for i in range(10):
     print(fib(i))
     
Output:
0
1
1
2
3
5
8
13
21
34

There are some disadvantages of recursive function. Sometimes logic behind the recursive function is hard. Recursive function takes lot of memory and time to run. Recursive function are hard to debug. 
In the next section we will see Built in function in python. 

Prev Topic  Next Topic

Comments

Popular posts from this blog

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Basic Knowledge of Python part 1

Basic Mathematics equations using Python- As we know python is a simple and easy to learn programming language. In this section we learn how to done mathematics equations using python. To perform this we use our basic operators +,-,/,* .  To perform python we have to open python interpreter. In this interpreter you can type simple equation like 5+5, 4/2 etc. For example: The integer number (e.g. 1,2,3....) have type int and the number with fractional part (e.g. 2.5,2.1,4.5...) have type float. Note:-  The division type always returns float. To get only an integer result of division you can use the // operator and to get remainder from division result you can use % operator. In python you can easily calculate power of the number by using ** operator. Like this you can easily calculate mathematics equation. Now we see how to find area of rectangle. Area = length*width is the formula to find area of rectangle. Here '=' sign is used to store value to the variable. You can als...

Python | Data Structure or Data Types (String)

Data Type Of Python I n the last session we discus about what is the data type of python. And we see List data type. In this session we s ee the next data type String.     2) String- String is a ordered collection of immutable data type. String are iterable and hashable. Strings are always declared in single or double quotes.  Single line string:- S = "Python" Multi line string:- S = """ Line 1             Line 2             Line 3 """ Iterable:- S= "Program" for i in S:       print(i) Output:-  P  r  o  g  r  a  m Membership:-  print( 'p' in S) => True print( 'P' not in S) => False Methods of string:- capitalize() :-  It return the capitalize version of the string. More specifically it make the first character have uppercase and rest lowercase. For example:- S = "hello World" print(S.capitalize()) => "Hello world" count(substring, start,en...