When programming, in many places you would have implemented a function that invokes or calls another function. However, when a function calls itself we call that recursion.
Let’s try to understand the concept through some simple recursive functions in Python.
In this article, you will be looking into,
- Recursion Definition, Advantages, Disadvantages
- Method-1 using the non-tail method
- Method-2 using the tail method
When programming, in many places you would have implemented a function that invokes or calls another function. However, when a function calls itself we call that recursion. Let’s try to understand the concept through some simple recursive functions in Python.
What is Recursion?
Recursion is a common mathematical and programming concept. It means that a function calls itself. We know that a function in Python can call other functions. It’s possible that the function will call itself. Recursive functions are the name for these types of constructs.
Advantages of Recursion:
- Using recursion, a difficult issue can be broken down into smaller sub-problems
- Recursion makes it easier to generate sequences than nested iteration
- The use of recursive functions makes the code look neat and tidy
Dis-advantages of Recursion:
- Debugging recursive functions is difficult
- The logic of recursion can be difficult to comprehend at times
- Recursive calls consume a lot of memory and time, making it expensive to use
Now that we understand the basic syntax and the definition of Recursion, let’s jump in and see a few examples where recursion can be used!!
Method-1 using the non-tail method
Example 1: Recursive Program to Get Factorial of a Given Number
We shall write a program to get the factorial of a number using a recursion function!!
Factorial is a positive number. It’s the sum of all positive integers less than or equal to the number you’re looking for. An exclamation mark (!) is used to indicate it.
For example, in this program let us take the number 5 so,
Factorial of 5 is 5! = 12345 = 120
#let us write recursive function for returning factorial of a numberdef recurse_fact(i):if i == 1:return ielse:return i*recurse_fact(i-1)#let us take user inputn=5#checking if the input is valid or notif n < 0:print("Please enter a positive number,Invalid Input!!")elif n == 0:print("Factorial of 0 is 1")else :print("Factorial of a number is:",n,"=",recurse_fact(n))
So we successfully found a factorial of a number using the recursion function!! Now, Let us write another recursive function to find the fibonacci Series!
You can also explore – Python Projects for Beginners
Method-2 using the tail method
Example 2: Recursive Program to Generate Fibonacci Series.
Fibonacci series is basically, 0,1,1,2,3,5….where 0 and 1 are the first two words. All other terms are formed by combining the two preceding terms. So, the nth term is equal to the sum of the (n-1)th and (n-2)th terms.
#let us write recursive function for returning fibonacci series for n_termsdef recurse_fibonacci (i):if i<=1:return ielse:return(recurse_fibonacci(i-1) + recurse_fibonacci(i-2))#let us define the termsn=8#checking if the number of terms are validif n<=0:print("Please enter a positive number,Invalid Input!!")else:print("Fibonacci Series for 8 terms are:")for j in range(n):print(recurse_fibonacci(j))
So we successfully found a fibonacci series of a given term using the recursion function!! In simpler terms, recursion is a programming technique in which a function calls itself.
You can also explore – Introduction to Python – Features, Use Cases, Resources, and Applications
Hope this article was helpful to make you understand what recursion is? And different ways to write recursion programs!
Download this article as PDF to read offlineDownload as PDF