Possible Duplicate:
Scala immutable variables and printing
I am confused why this will not run as i expect it to, I have tried to change it multiple times and it still give an error stating that it is given Unit and needs Int.
def div(m: Int, n: Int): Int = {
var counter = 0
var p = m
while (p >= 0) {
p -= n
counter += 1
println(counter)
}
}
In scala result of last operation would be return value. In your piece of code last operation is while loop that doesn't return anything (or, if you want, return Unit which is the same as nothing) while you specified return value of Int.
So if you need to return value of p write this:
def div(m: Int, n: Int): Int = {
var counter = 0
var p = m
while (p >= 0) {
p -= n
counter += 1
println(counter)
}
p
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With