Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

A function is a number of instructions grouped together.
A function has a name, input parameters and a return value.
In scala there are two types of functions: funtion and method.

Method

an example of a simple function in Scala that takes two integers as parameters and returns their sum:


def add(x: Int, y: Int): Int = 
  x + y
  
// Example usage
val result = add(3, 5) // 8

In this example, the add function takes two parameters of type Int, named x and y. The function body simply adds these two parameters together and returns the result as an Int. The function definition ends with a colon followed by the return type of the function, which in this case is Int. Scala supports type inference, so in simple cases like this, you can often omit the return type and let the compiler figure it out.

Function

In Scala, a val can hold a function as a value. Here's an example of defining a function as a val:

val add: (Int, Int) => Int = (x, y) => x + y

// Example usage
val result = add(3, 5) // 8

In this example, we define a val named add which has the type (Int, Int) => Int. This means that add is a function that takes two parameters of type Int and returns a value of type Int.

The right-hand side of the = sign is a function literal, which is an anonymous function that takes two parameters x and y, adds them together, and returns the result.

Because the Scala compiler can usually infer the types of function parameters and return values, we can usually omit the type annotations and simply write:

val add = (x: Int, y: Int) => x + y

Default Parameters

Scala allows you to specify default values for function parameters. If the caller omits those parameters, Scala uses the default values.

def greet(name: String, greeting: String = "Hello"): String = s"$greeting, $name!"

println(greet("Alice")) // Output: Hello, Alice!
println(greet("Alice", "Hi")) // Output: Hi, Alice!

Named Arguments

When calling functions, you can specify arguments by name, rather than strictly by position. This is particularly useful when a function has multiple parameters with default values.

def describePerson(name: String, age: Int, country: String = "Unknown"): String =
  s"$name is $age years old from $country."

println(describePerson(age = 30, name = "Bob")) // Output: Bob is 30 years old from Unknown.

Variable Arguments (Varargs)

Scala allows you to define functions that take an arbitrary number of arguments of the same type. This is achieved using the * syntax.

def sum(nums: Int*): Int = nums.sum

println(sum(1, 2, 3, 4, 5)) // Output: 15

Anonymous Functions (Function Literals)

Scala allows the definition of anonymous functions, which are handy for short snippets of code that are passed to higher-order functions or used for creating quick and simple function objects.

val addOne = (x: Int) => x + 1
println(addOne(9)) // Output: 10