Recursion vs. Tail Recursion vs. Loop | Sololearn: Learn to code for FREE!


Recursion vs. Tail Recursion vs. Loop

Suppose you are working for a software company having a system with limited memory and you have been given a task. You are using a recursive function to complete the task but you realize that you can use tail recursion instead of a recursive function and then you realize that this tail recursion can be converted to loop. Now the question is that which of the following technique is best for the above-given scenario and why? 1. Recursive Function. 2. Tail Recursion. 3. Loop.

8/2/2019 1:59:04 PM

Ayesha Zareen

2 Answers

New Answer


Interesting question. The limited memory makes recursion a bad option due to each recursive call needing a new scope to store the next state. As for tail recursion vs iteration (or loops as you call it)? Well this depends on if the compiler you're using does tail recursion optimizations. If yes, then tail recursion makes a lot of sense for the circumstances since you will get the benefits of recursion and the memory efficiency of iteration (people have long said a while loop is a simple example of tail recursion).


I know Scheme uses Tail recursion to implement all looping. this eliminates a lot of unnecessary stack frame generation of regular recursion