Recursive Functions

When a function calls itself, this process is called Recursion. Let’s understand this concept using the most used example of finding the factorial of a given number. Mathematically, the factorial of a number is given by multiplying the number with its decreasing value, one at a time, till 1. For example, the factorial of 5 is written as 5 ! = 5 x 4 x 3 x 2 x 1. If we observe this, we can say that it’s 5 x 4 !  Because 4 ! = 4 x 3 x 2 x 1. Again, it can be written as 4 x 3 !,  because 3 ! = 3 x 2 x 1. Again, it can be written as 3 x 2 !,  because 2 ! = 2 x 1. Again, it can be written as 2 x 1 !,  because 1 ! = 1 x 1. In generalized form we can write it as, n x (n-1) !. Starting with n = 5, it will first call (n-1) !, that is 4 !. Now the new value of n will be 5 x 4 x (n-1) !., then it will again be 3 !. Now the new value of n will be 5 x 4 x 3 x (n-1) !, then, it will again call 2 !, and finally, it will again call 1 !. This is how a function calls itself. Let’s code this.

So, we can define recursion as a process in which a function breaks down into smaller problems, and it keeps calling itself for each of the smaller problems until a base case is reached. Like here, the base case is 1 !. What does a non- recursive or general iterative factorial function look like? 

It’s a general function. There are some differences between iteration and recursion.

  • Recursion requires more memory.
  • In many languages Iteration is a much faster approach.
  • Recursion sometimes can be a more abstract and harder approach to understand.
  • Recursion is practically a faster method for applications like traversing trees and binary search.

Is there a limit to which a function can call itself? What’s the difference between a function which can call itself for infinite times and a recursive function? Let’s call a function recursively without having a base or terminal case and see if it’s an infinite function or has some limit.

The function endgame () is calling itself again and again and we are not providing a limit. When I executed this code, the output I got was a lot of Avengers Assemble printing continuously and then after sometime an error occurred.

Recursion Error, and it says that OK there is some maximum recursion depth. Is there a default value? For this recursion provides a method called .getrecursionlimit (). We have to import a module called sys. Let’s see how to use it and what value is generated, if any default value is there.

Look at the first line of the output. It’s displaying a value of 3000. This may be our default value. We can also set this limit by using another method called  .setrecursionlimit (). We can pass the value up to which we want that the function calls itself.

The value is now 2000 as set. I am also using a global variable i, to keep a count of my output numbers generated. So, basically Recursion is a desired condition and accidently if it enters an infinite loop condition, a default value has been set to avoid our program from being crashed or the system being hanged.

Design a site like this with WordPress.com
Get started