Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What is the difference between recursion and tail recursion?


Asked by Corey Vo on Dec 10, 2021 FAQ



In simple, the main difference between the traditional recursion and tail recursion is when the actual calculation takes place. In traditional recursion, calculation will happen after the recursion call while the calculation will occur before the recursion call in tail recursion.
Accordingly,
A tail recursive function in Scala is remedy if your recursive functions causes a stack overflow. Furthermore, tail recursion is a great way if to make your code faster and memory constant. With this in mind, let's dive into how tail recursion can be implemented in Scala.
One may also ask, Tail recursion is the act of calling a recursive function at the end of a particular code module rather than in the middle. A function is recursive if it calls itself.
And,
Recursion are mainly of two types depending on weather a function calls itself from within itself weather two function call one another mutually. The former is called direct recursion and t latter is called indirect recursion. Thus, the two types of recursion are: Direct recursion. Indirect recursion.
Also Know,
The tail recursive functions better than non tail recursive functions because tail-recursion can be optimized by compiler. A recursive function is said to be tail recursive if the recursive call is the last thing done by the function. There is no need to keep record of the previous state.