Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Tail Recursion in Scala [Video]

DZone's Guide to

Tail Recursion in Scala [Video]

Sure, recursion can be error-prone, but that's what tail recursion tries to solve. See how Scala helps developers out with recursive code.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Recursion is quite common in the programming world. As you probably know, it's the process of solving a problem by breaking it down into smaller subproblems. You can easily spot recursion if you see a method calling itself with a smaller subset of inputs.

Why Recursion?

Many programmers consider recursion tough and error-prone, but in Scala, we use recursion because:

  • It avoids the mutable variables that you need to use while writing loops.
  • It is a natural way of describing many algorithms.

Problems With Recursion

Even though recursion can help you eliminate mutable variables from code, there are still some problems with it. As a developer you should take care of:

  • Memory consumption in each recursive call.
  • The fact that deep recursion can blow up your stack.

Tail Recursion

In order to avoid the problems of recursion, we can use tail recursion. So, the first question is, "What is tail recursion?" If the recursive call is the last operation performed by the function, and no operations need to be saved when the function returns, that is called tail recursion.
If there is nothing on the stack frame, the compiler can reuse the stack frame and will convert the recursion into a loop.

Scala Support of Tail Recursion

Scala can guide developers through tail recursion. You can use @tailrec to check if the recursion is tail recursive or not. The annotation is available as a part of the scala.annotation._ package. If the recursion is not tail recursive, then Scala will throw a compile-time error.

People who are really keen on hands-on examples can refer to this YouTube link or watch the video below.


Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
scala ,tail recursion ,java ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}