Functions and Methods

In Scala, functions are first-class citizens. You can define them inside objects or pass them around like values.

Defining a Method

def add(a: Int, b: Int): Int = a + b

Anonymous Functions

val multiply = (x: Int, y: Int) => x * y
println(multiply(2, 3))

Higher-Order Functions

Functions can take other functions as parameters:

def applyFunc(f: Int => Int, x: Int): Int = f(x)
println(applyFunc(_ + 1, 5))
← PrevNext →