Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
def triple(x: Int) = 3 * x // Parameter name: Type val f = (x: Int) => 3 * x // Anonymous function (1 to 10).map(3 * _) // Function with anonymous parameter def greet(x: Int) { // Without =, return type is Unit println("Hello, " + x) } def greet(x: Int, salutation: String = "Hello") { // Default argument println(salutation + ", " + x) } // Call as greet(42), greet(42, "Hi"), greet(salutation = "Hi", x = 42) def sum(xs: Int*) = { //* denotes varargs var r = 0; for (x <- xs) r += x // Semicolon separates statements on same line r // No return. Last expression is value of block } def sum(xs: Int*): Int = // Return type required for recursive functions if (xs.length == 0) 0 else xs.head + sum(xs.tail : _*) // Sequence as varargs