We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is a Tail Recursion?

By Jessica Susan Reuter
Updated: May 16, 2024

Tail recursion is a type of programming method call where a method calls itself, then immediately returns the value of that second call. In other words, tail recursion occurs when the final statement inside a method is another call to that same method. The parameters in the second method call are generally different from those of the first, but this is not required. In order for this recursion to work, the method that is called within itself must return a concrete value, such as a number, string, or some other object. Void methods, which do not return a value, do not work well for recursion.

The requirement that a recursive call must be the last statement in its calling method does not necessarily mean that the recursive call is the last line in the method. A proper tail recursion call can also be found inside a control structure, which means that, in source code, the control structure may end the method rather than the call. The important distinction in this case is that a control structure is not a programming statement, but a built-in part of the computer language.

Tail recursion exists in many computer languages, including Java and C++. Frequently, these recursive calls can be rewritten using other means, such as for loops, while loops, or goto statements. The utility of recursion is found when creating many sequential calls to the same method. Recursion is often the cleanest and easiest way to accomplish repetitive tasks.

A common example of tail recursion is a method that calculates the factorial of a number. This process is ideal because, starting at any number, every number before it is multiplied together. So, to find the factorial of 5, the proper process to do that would be to multiply 5*4*3*2*1. The recursion comes in because of how the factorial method is structured: if the factorial is 1, return 1, otherwise return the factorial of the number given to the method minus one. This method is also useful because it can be equivalently written using either type of tail recursion, with or without a control statement around a final method call.

Tail recursion is just one example of the multiple types of recursion. The concept in all types of recursion is essentially the same, that in some fashion a method calls itself. Of these types, the distinction of tail recursion is that the value of a recursive call is immediately returned, and nothing else happens in the calling method after that call.

EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
https://www.easytechjunkie.com/what-is-a-tail-recursion.htm
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.