Scala Recursion with example

admin

6/20/2023
All Articles

#undefined

Scala  Recursion with example

Scala  Recursion with example

 
In this guide, we will discuss Scala Recursion Functions.
Recursion plays a big role in pure functional programming and Scala supports recursion functions very well.
Recursion means a function can call itself repeatedly.
 
There is normal recursion 
 
def factorial(n: Int): Int =
  if (n == 0) 1 else n * factorial(n - 1)
  
  
There is tail recursion,
One can require that a function is tail-recursive using a @tailrec annotation:
 
  @tailrec
def factorial(n: Int): Int =