Previous post: Next post:
Python #11-Functions in Python Python #13-Let's Play with Python part-1-Fibonacci Series
Example:
def decreasingorder(num):
if num==0:
print "values is Zero"
else:
print num
decreasingorder(num-1)
decreasingorder(2) #calling function with argument 2
Here decreasing order is the recursive function. I have called the function inside the same function. Inside the else. I have called the decreasing order function to perform the operation.
Python #11-Functions in Python Python #13-Let's Play with Python part-1-Fibonacci Series
What is recursive function?
A recursive function is a function which is called by itself..
Example:
def decreasingorder(num):
if num==0:
print "values is Zero"
else:
print num
decreasingorder(num-1)
decreasingorder(2) #calling function with argument 2
Here decreasing order is the recursive function. I have called the function inside the same function. Inside the else. I have called the decreasing order function to perform the operation.
Program Explanation:
First of the Input to the function should be passed.
For eg:
I have passing (2) to the decreasingorder function. It will check whether given number is ==0 or not. if its is zero then we cant to perform operation so it will print "Value is Zero". Since we have given 2 it will go to the else part.There it will print the num 2 then it comes to the statement decreasing order (num-1)here the decreasing order the function where i have called again...then it will again the if condition will be check whether the number is ==0 or not if true it will print "Value is Zero". Now in our case it will goes to else part and print the print the num 1 because(2-1=1)once its is print it again come to the decreasing order (num-1) line. now the value will will be zero. So the function will be automatically called and again the if will be check now the if statement will be true. So it will print "Value is Zero" and the program will be ended.
Don't Forget to Try it
Comments