Can anybody please help me with this exercise, I have gone through it a million times and I cannot understand how the output can be 42. I especially struggle with val bp. A walkthrough the code would be highly appreciated. Thanks a lot!
object Scope {
var a : Int = 1
var b : Int = 3
var c : Int = 0
def m1(b : Int) : Int = {
a = 5
c += a + b
b + 3
}
def m2(a : Int, d : Int) : Int = {
b = c
val bp = m1(a - d)
c += b + bp
b + c
}
def main(args : Array[String]) : Unit = {
b = m1(a)
a = m2(b,a)
c += a
println(c)
}
}
m1orm2. Do yourself a favor and throw out whatever book you found this exercise in, it's not teaching you anything useful.m1'sbparameter is notvar b,m2'sais notvar aand so on. As @DmytroMitin said use debugger - it will show you how variables are initalized and modified step by step.