I am actually learning scala and I have a question about tail-recursion. Here is an example of factorial with tail recursion in scala :
def factorial(n: Int): Int = { @tailrec def loop(acc: Int, n: Int): Int = { if (n == 0) acc else loop(n * acc, n - 1) } loop(1, n) } My question is updating the parameter, acc as we do it in the function loop can be considered as a side effect? Since in FP, we want to prevent or diminish the risk of side effect.
Maybe I get this wrong, but can someone explain to me this concept.
Thanks for your help
@tailrecshould be moved to yourloopfunction. Functionloopis recursive, whilefactorialis not - it never calls itself, it only callsloop.