Functions and Methods
Local Functions
Define a helper inside another method.
local-functions
A local function belongs to the block where it is defined. It keeps a helper close to the code that uses it.
Local Functions
LocalFunctions.scala
Replay: real traced execution (multi-file project)
object Main {
def main(args: Array[String]): Unit = {
val base = 3
def addOne(value: Int): Int = {
value + 1
}
val result = addOne(base)
println("base=" + base)
println("result=" + result)
}
}
object Main {
def main(args: Array[String]): Unit = {
val base = 5
def addOne(value: Int): Int = {
value + 1
}
val result = addOne(base)
println("base=" + base)
println("result=" + result)
}
}
base ← 3
1object Main {2 def main(args: Array[String]): Unit = {3 val base→ 3 = 3 //@base=545 def addOne(value: Int): Int = {6 value + 17 }89 val result = addOne(base3)1011 println("base=" + base)def addOne(value: Int): Int =
5def addOne(value3: Int): Int = {6 value3 + 17}result ← 4
9 val result→ 4 = addOne(base3)1011 println("base=" + base3)12 println("result=" + result4)13 }14}outputbase=3 result=4
base ← 5
1object Main {2 def main(args: Array[String]): Unit = {3 val base→ 5 = 545 def addOne(value: Int): Int = {6 value + 17 }89 val result = addOne(base5)1011 println("base=" + base)def addOne(value: Int): Int =
5def addOne(value5: Int): Int = {6 value5 + 17}result ← 6
9 val result→ 6 = addOne(base5)1011 println("base=" + base5)12 println("result=" + result6)13 }14}outputbase=5 result=6