Skip to main content

tailrec Functions in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

tailrec Functions is a fundamental concept every Kotlin developer should understand. Functions are first-class in Kotlin — parameters, return types, and concise syntax make APIs easy to read and reuse.

Tailrec marks a recursive function that calls itself as the last operation. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • tailrec marks a recursive function that calls itself as the last operation.
  • The compiler optimizes tailrec functions into an efficient loop.
  • Use tailrec instead of manual loops for clear recursive algorithms like factorial.

Syntax

tailrec fun factorial(n: Int, acc: Int = 1): Int

tailrec Functions in Kotlin Example Program in Kotlin

tailrec fun factorial(n: Int, acc: Int = 1): Int {
    return if (n <= 1) acc else factorial(n - 1, acc * n)
}

fun main() {
    println("5! = ${factorial(5)}")
}

Sample Output

5! = 120

When to use

Use default and named parameters to reduce overloads; use single-expression functions for small pure helpers.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. Tailrec marks a recursive function that calls itself as the last operation.

  3. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Understand the core idea: tailrec marks a recursive function that calls itself as the last operation.
  • Prefer readable names and small functions so examples map directly to real projects.
  • Run and modify the example — change values and observe how the output changes.

Common Mistakes

  • Skipping the example and only reading the definition — hands-on practice cements the concept.
  • Copying syntax without understanding nullable vs non-nullable types or scope rules.
  • Ignoring compiler warnings that often point to safer alternatives.

Key Points

  • tailrec marks a recursive function that calls itself as the last operation.
  • The compiler optimizes tailrec functions into an efficient loop.
  • Use tailrec instead of manual loops for clear recursive algorithms like factorial.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is tailrec Functions in Kotlin?
Tailrec marks a recursive function that calls itself as the last operation.
When should I use tailrec Functions?
Use default and named parameters to reduce overloads; use single-expression functions for small pure helpers.
How is tailrec Functions different from Java?
Use tailrec instead of manual loops for clear recursive algorithms like factorial.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials