In Scala, you define functions using the def
keyword.
def add(x: Int, y: Int): Int = {
x + y
}
val sum = add(5, 3) // Calling the function
println(sum)
Scala functions can have parameters and return types. You must specify the type of each parameter.
def multiply(x: Double, y: Double): Double = {
x * y
}
Scala supports default and named parameters.
def greet(name: String = "World", greeting: String = "Hello") = {
println(s"$greeting, $name!")
}
greet() // Hello, World!
greet(name = "Alice") // Hello, Alice!
greet(greeting = "Hi", name = "Bob") // Hi, Bob!
Higher-order functions are functions that take other functions as parameters or return functions.
def operate(x: Int, y: Int, f: (Int, Int) => Int): Int = {
f(x, y)
}
def add(x: Int, y: Int): Int = x + y
val result = operate(5, 3, add) // result is 8
Anonymous functions are functions without a name.
val multiply = (x: Int, y: Int) => x * y
val product = multiply(4, 2) // product is 8
Partially applied functions and currying are techniques for creating new functions from existing ones.
def add(x: Int, y: Int, z: Int) = x + y + z
val add5 = add(5, _: Int, _: Int) // Partially applied function
val sum =