Posts

Showing posts with the label Recursive

How to Write Python Recursive Function

Image
Recursive functions act like loops that they iterate within the function to perform some operation.  Since you are calling the same function in the function definition, it's called recursive . The developer must follow some rules to write a recursive function.  Else, it will go into a loop. So developers must check twice the recursive function code. Four rules Here are the four top rules you need to consider, before you write a recursive function. There must be a key variable, which will be responsible for the termination of recursion. To determine the base value , which the key variable has to meet to reach the termination. To make sure the key variable must approach the base value in every recursive call. To make the recursive function terminate when the key variable reaches the base value. Python recursive function Here is a sample recursive function: # This program computes the factorial of a number using recursion #function definition def fact(n): "computes factorial us