I'm learning scala and I have come across the following code.
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
if (cond) {
body
whileLoop(cond)(body)
}
var i = 10
whileLoop (i > 0) {
println(i)
i -= 1
}
The output is the numbers 10 to 1.
So both cond and body are "call by name" parameters. Which means they are evaluated when used in the function. If I understand that correctly. What I don't understand is how the body
println(i)
i -= 1
changes for each level of recursion that is applied the body is changing as the variable i is changing. But how does that exactly work? Each time the same function body is passed, to me this function stays the same but running the program shows me otherwise. I know that the function is evaluated each time but I don't understand how the i variable inside changes each time so can someone explain me how that works?