Kotlin Loops in Kotlin Recursion

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Looping via recursion is also possible in Kotlin as in most programming languages.

fun factorial(n: Long): Long = if (n == 0) 1 else n * factorial(n - 1)

println(factorial(10)) // 3628800

In the example above, the factorial function will be called repeatedly by itself until the given condition is met.



Got any Kotlin Question?