Scala

33 / 51
   

Scala - Closures

A closure is a function, whose return value depends on the value of one or more variables declared outside this function.
A closure also works as a variable.

Example 1:

val multiplier = (i:Int) => i * 10

Example 2:

var factor = 3
val multiplier = (i:Int) => i * factor

Example 3:

object Hello {
   def main(args: Array[String]) {
      println( "multiplier(1) value = " +  processme(1) )
      println( "multiplier(2) value = " +  processme(2) )
   }
   var factor = 4
   val processme = (i:Int) => i * (factor+1)
}