So, in this section, we are going to learn about Recursion and will also see some real-life examples of Recursion. We will also discuss that when we should use Recursion and when we should avoid its use.
What is Recursion and Recursive Function
The syntax for a recursive function is given below:
One should note that a recursive function must have a condition so that it should stop its execution; otherwise, the function will be invoked and executed for infinite times.
When to use Recursion
Recursion is capable of breaking down large and complex problems into small problems. However, not every complex problem can be solved via Recursion. The use of the recursive function is most appropriate and effective for problems like iterative branching, such as sorting, traversing, binary search, and other data structures. Also, it is good to use recursive functions when there is a need to invoke the same function again and again but with different parameter values within a loop. For example, finding factorial of a number (a large number) using Recursion, Fibonacci series implementation, Tower of Hanoi are the best and easy to solve through Recursion.
When to avoid using Recursion
One should avoid solving a problem with Recursion in case the problem is too small and can be solved with just a few lines of simple code. It is because recursive functions invoke itself unless it is not stopped. Thus it uses a good amount of memory unnecessarily. Therefore, when we are solving a problem that can be solved without the use of Recursion, avoid using it. Sometimes, there may be a case that if we misuse Recursion, the whole program may get infinite, and there would be no better option than terminating the program. So, use Recursion wherever needed and with correctness.
Implementing Real-Life Examples of Recursion
Generally, Recursion is the favorite topic for the interviewers as they often interrogate for recursion questions. There are various examples of our real-life where Recursion is used:
Example1: Search Algorithms
There are many search algorithms we have learned where recursion is used such as in binary search. So, below code shows the use of recursion in binary search:Test it Now
There is an alternative method also available for solving binary search without Recursion but traversing and iterating the whole array for searching the element is complex and time-consuming. Thus, it becomes simple to solve it using Recursion.
Example 2: Delay Timers
Suppose after a set period of time we want to execute a function for multiple times, it is the best option to make use of recursion. So, let's see the code implementation for the same:Test it Now
In the above code,
Example 3: Puzzle Solving
It is the best option to choose Recursion for solving puzzles because Recursion can help to get the best-optimized solution for the puzzle. There are many puzzles we may play in our day-to-day life, such as tic-tac-toe. One must know how typical it would be if we try to solve with an iterative solution because for every move, we need to make several consideration factors - a complex task. However, it becomes easy or less complex to find the best move via a recursive algorithm. Thus, not only for tic-tac-toe but also in those games where you know that there are multiple iterative moves and need to determine the best move, Recursion is the best way for it.
Example 4: Fractal Designs
For solving the fractal designs, we can make use of Recursion. Fractal designs/patterns are the designs that are defined recursively. These fractal designs look as:
The pattern consists of several complex steps which are difficult to solve. But when we try to solve the fractal design with the recursion method, we can judge and see the output of each particular step.
Example 5: Inductive Proofs
An inductive proof is a composition of base case and inductive steps where:
For example: Climbing a ladder
Similarly, there are more examples of inductive proofs that can be solved using Recursion.
Thus, all these are the real-world examples of Recursion and the examples where we use the Recursion.