In this tutorial, we will learn Kotlin Recursive function. Like other programming languages, we can use recursion in Kotlin. A function that calls itself is called a recursive function and this process of repetition is called recursion. Whenever a function is called then there are two possibilities:
- Normal function call
- Recursive function call
Normal function call
When a function is called from main() block then it is called a normal function call. In below example, sum() is called at a time and it executes its instruction and terminate with returning the sum of number. If we want to execute the function again then we should call sum() from the main block one more time.
Calling sum() function from main() block –

Recursive Function Call
When a function calls itself then it is called recursive function call. Every recursive function should have terminate condition else program executions enters in infinite loop and results into stack overflow error.
Calling callMe() function from its own block –

Here, we have used the terminate condition if( a > 0) else it enters the infinite loop. And it prints the value from 5 down to 0.
Example 1: Find the factorial of a number without using the terminate condition.
Java
fun Fact(num: Int):Long{
return num*Fact(num- 1 )
}
fun main()
{
println( "Factorial of 5 is: " + Fact( 5 ))
}
|
Output:
Exception in thread "main" java.lang.StackOverflowError
Example 2: Find the factorial of a number with using terminate condition.
Java
fun Fact(num
: Int)
: Long
{
return if (num == 1 ) num.toLong()
else num
* Fact(num - 1 )
}
fun main()
{
println( "Factorial of 5 is: " + Fact( 5 ))
}
|
Output:
Factorial of 5 is: 120
Working of factorial program –
The recursive call of Fact() is explained step by step in the following figure:-
Example 3: Find the sum of elements of an array using recursion
Java
fun sum(args
: Array<Int>, index
: Int)
: Int
{
return if (index <= 0 ) 0 else (
sum(args, index - 1 )
+ args[index - 1 ])
}
fun main()
{
val array = arrayOf( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 )
val n
= array.size val result
= sum(array, n)
println( "The sum of array elements is: $result" )
}
|
Output:
The sum of array elements is: 55
Explanation: Here, we have initialized an array and passed as an argument to the sum() function. In each recursive call the index value decrement by one. If the index equal to zero or less than then terminate it and return the sum of all the elements.
Advantages of recursion in Kotlin:
- Improved code readability and maintainability: Recursive functions often make the code more concise and easier to understand, especially for complex problems.
- Easy implementation of algorithms: Many algorithms, such as the Tower of Hanoi, can be easily implemented using recursion.
- Reusability: Recursive functions can often be reused for similar problems, reducing the need to write new code.
Disadvantages of recursion in Kotlin:
- Increased memory usage: Each recursive call adds a new function call to the call stack, which can lead to stack overflow errors for large data sets.
- Performance: Recursive algorithms are often slower than iterative algorithms, as they require more memory and CPU cycles.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Feb, 2023
Like Article
Save Article