In this article, you will learn how to create recursion functions in kotlin programming language and also learn tail recursion. Recursion functions means it calls itself continuously.
This kind of process is called as recursion. In normal recursion, we perform all recursive calls first then we calculate result from values. But in tail recursion, first calculations are performed then calls are executed.
For example: Calculate factorial of number using recursion function
Process of above example:
To perform tail recursion in kotlin, we need to give tailrec modifier to the function so compiler can identify. In this process in the function last statement is a recursive call which is executed by same function.
I already explained in this method first we perform calculations then recursive calls are executed. We already calculated factorial of number using recursion. But here we calculate with tail recursion instead of normal recursion.
For example: Calculate factorial of number using tail recursion function