Scala Functions as Values

def square(n: Int): Int = n * n defines a method whose body is a single expression. In Scala functions are values: a function of type Int => Int can be stored in a variable or passed as an argument.

Scala
object Main {
  def square(n: Int): Int = n * n

  def main(args: Array[String]): Unit = {
    val twice = (f: Int => Int, x: Int) => f(f(x))
    println(square(5))
    println(twice(square, 3))
  }
}
Read the full lesson