Kotlin Tail Recursion Last Updated : 18 May, 2025 Comments Improve Suggest changes 11 Likes Like Report In a traditional recursion call, we perform our recursive call first, and then we take the return value of the recursive call and calculate the result. But in tail recursion, we perform the calculation first, and then we execute the recursive call, passing the results of the current step to the next recursive call. In the end, both recursion and tail recursion give the same output. The must-follow rule for tail recursion is that the recursive call should be the last call of the method.Benefits of using tail recursionIn tail recursion, the function call is the last thing executed by the function, and nothing left in the current function to execute. So, there is no need to save the current function call in the stack memory, and the compiler can reuse that stack space for the next recursive call.In tail recursion, we do not get the StackOverflowError during the execution of the program.Example 1: Find the factorial of a number using tail recursion Kotlin // Kotlin program of factorial using tail-recursion fun Fact(num: Int, x:Int):Long{ return if(num==1) // terminate condition x.toLong() else Fact(num-1,x*num) //tail recursion } fun main() { var n = 1 var result = Fact(5,n) println("Factorial of 5 is: $result") } Output:Factorial of 5 is: 120Working of the above programExample 2: Find the sum of elements of an array using tail-recursion Kotlin // two parameters passed an array and size of array fun sum(args: Array<Int> , index:Int, s : Int = 0 ):Int{ return if(index<=0) s else sum(args ,index-1, s + args[index-1]) // tail-recursion } fun main() { // array initialization val array = arrayOf(1,2,3,4,5,6,7,8,9,10) // size of array val n = array.size val result = sum(array,n) // normal function call println("The sum of array elements is: $result") } Output:The sum of array elements is: 55Explanation: Here, we have passed the array as an argument along with two other parameters to the sum() function. The default value of the (s) parameter is equal to zero. We are calculating the sum of elements from the last index of the array, with each recursive call. With the last recursive call, we will have the sum of all elements in s and return it when the condition is satisfied. Create Quiz Comment P Praveenruhil Follow 11 Improve P Praveenruhil Follow 11 Improve Article Tags : Kotlin Kotlin Basics Explore OverviewIntroduction to Kotlin4 min readKotlin Environment setup for Command Line2 min readKotlin Environment setup with Intellij IDEA2 min readHello World program in Kotlin2 min readBasicsKotlin Data Types3 min readKotlin Variables2 min readKotlin Operators4 min readKotlin Standard Input/Output4 min readKotlin Type Conversion2 min readKotlin Expression, Statement and Block4 min readControl FlowKotlin if-else expression4 min readKotlin while loop2 min readKotlin do-while loop2 min readKotlin for loop4 min readKotlin when expression6 min readKotlin Unlabelled break4 min readKotlin labelled continue4 min readArray & StringKotlin Array6 min readKotlin String4 min readFunctionsKotlin functions7 min readKotlin Default and Named argument7 min readKotlin Recursion3 min readKotlin Tail Recursion2 min readKotlin Lambdas Expressions and Anonymous Functions6 min readKotlin Inline Functions5 min readKotlin infix function notation5 min readKotlin Higher-Order Functions6 min readCollectionsKotlin Collections6 min readKotlin list : Arraylist6 min readKotlin list : listOf()7 min readKotlin Set : setOf()4 min readKotlin hashSetOf()4 min readKotlin Map : mapOf()5 min readKotlin Hashmap7 min readOOPs ConceptKotlin Class and Objects4 min readKotlin Nested class and Inner class3 min readKotlin Setters and Getters4 min readKotlin Class Properties and Custom Accessors3 min readKotlin Constructor6 min readKotlin Visibility Modifiers6 min readKotlin Inheritance10 min readKotlin Interfaces7 min readKotlin Data Classes3 min readKotlin Sealed Classes4 min readKotlin Abstract class5 min readEnum Classes in Kotlin4 min readKotlin extension function4 min readKotlin generics6 min readException HandlingKotlin Exception Handling - try, catch, throw and finally5 min readKotlin Nested try block and multiple catch block3 min readNull SafetyKotlin Null Safety7 min readKotlin Type Checking and Smart Casting3 min readKotlin Explicit Type Casting3 min readRegex & RangesKotlin Regular Expression4 min readKotlin Ranges3 min read Like