Skip to content
LumenaryAcademy
Foundations 55 min

Functions and methods

Methods with def, functions as values, higher-order functions and closures, recursion, and parameters evaluated by name.

Code tested · 4AI-draftedAwaiting review by a computer scientist
MotivationH

Two kinds of function

Every language lets you name a piece of computation and reuse it. Scala gives you two closely related tools for that, and knowing which is which clears up a lot of early confusion.

A method, defined with def, is what you have used so far: def isLeap(year: Int): Boolean = .... Methods belong to something (a class, an object, or the top level of a file), and they are the main way to organise code.

A function value is a function you can hold in your hand: store it in a val, put it in a list, pass it to another method, return it as a result. (n: Int) => n * 2 is a function value. Functional programming, one half of Scala's personality, rests on the fact that functions are ordinary values.

Most of the time you will not have to think about the difference, because Scala converts a method into a function value automatically whenever one is needed: when you wrote years.count(isLeap) in lesson 1, the method isLeap became a function value and was passed to count. This lesson teaches both tools, how they meet, and the habits experienced Scala programmers follow when writing them: small, well-named, mostly pure functions with explicit types at their boundaries.

DefinitionsH

Vocabulary

Definition— Method

A method is declared with def name(params): ResultType = body. Parameter types are always written. The result type can usually be inferred, but writing it on any method others will call is good practice: it documents the method and keeps type errors close to their cause. A recursive method must declare its result type.

Definition— Function value and function type

A function value (also called a lambda or anonymous function) is written (params) => body, for example (a: Int, b: Int) => a + b. Its function type is written with an arrow: (Int, Int) => Int takes two Ints and returns an Int; Int => Int takes one; () => Int takes none.

Definition— Higher-order function

A higher-order function takes functions as parameters or returns a function. map, filter and count on collections are higher-order methods.

Definition— Closure

A closure is a function value that refers to names from the scope where it was created, such as a parameter of the method that built it. The function keeps those names alive and usable after that method has returned.

Definition— Pure function

A function is pure if its result depends only on its arguments and calling it has no side effects (no printing, no mutation of outside state, no I/O). A pure call can be replaced by its result without changing the program, which makes pure code easy to test and reason about.

Worked examplesH

Methods in practice

Default, named and repeated parameters, and parameter lists

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

def average(xs: Double*): Double =
  if xs.isEmpty then 0.0 else xs.sum / xs.size

def withTax(amount: BigDecimal)(rate: BigDecimal): BigDecimal =
  amount * (1 + rate)

@main def methods(): Unit =
  println(greet("Aiyana"))
  println(greet("Tomás", "Hola"))
  println(greet("Hiroshi", punctuation = "."))
  println(greet(greeting = "Karibu", name = "Wanjiru"))
  println(average(3, 4, 8))
  println(average())
  val marks = List(70.0, 85.0, 91.0)
  println(average(marks*))
  println(withTax(200)(0.15))
  val addVat = withTax(80)
  println(addVat(0.2))
text
Hello, Aiyana!
Hola, Tomás!
Hello, Hiroshi.
Karibu, Wanjiru!
5.0
0.0
82.0
230.00
96.0
  • Default arguments (greeting: String = "Hello") can be left out by the caller.
  • Named arguments (punctuation = ".") let the caller skip some defaults or pass arguments in any order. Teams use them freely for Boolean and numeric arguments, because resize(width = 800, keepRatio = true) explains itself and resize(800, true) does not.
  • A repeated parameter (xs: Double*) accepts any number of arguments, which arrive as a sequence. To pass an existing collection, spread it with marks*. (Scala 2 wrote this marks: _*, which you will see in older code.)
  • A method can have several parameter lists. withTax(80) supplies only the first and gives back a function waiting for the second; here it becomes addVat. Multiple parameter lists are also how Scala lets a final argument be a block of code, as you will see below.
  • withTax uses BigDecimal, as amounts of money should. With Double, 200 × 1.15 comes out as 229.99999999999997.

Recursion

A recursive method calls itself. Each call waits on the JVM's call stack for the one it made to return, so a recursion thousands of levels deep can fail with a StackOverflowError. The standard cure is to make the recursive call the very last thing the method does, a tail call, which the compiler can turn into a loop.

scala
import scala.annotation.tailrec

def factorial(n: Int): BigInt =
  if n <= 1 then 1 else factorial(n - 1) * n

@tailrec
def gcd(a: Int, b: Int): Int =
  if b == 0 then a else gcd(b, a % b)

def digitSum(n: Long): Int =
  @tailrec
  def loop(rest: Long, acc: Int): Int =
    if rest == 0 then acc
    else loop(rest / 10, acc + (rest % 10).toInt)
  loop(math.abs(n), 0)

@main def recursion(): Unit =
  println(factorial(25))
  println(gcd(1071, 462))
  println(digitSum(9_876_543_210L))
text
15511210043330985984000000
21
45
  • factorial is not tail-recursive: after the recursive call returns, it still has to multiply by n. That is fine for 25, not for a million.
  • gcd (Euclid's algorithm) is tail-recursive: the recursive call is the whole result. The @tailrec annotation asks the compiler to prove it: if the call were not in tail position, compilation would fail instead of leaving a stack overflow for later. Put @tailrec on every method you intend to be tail-recursive.
  • digitSum shows the usual trick for making a recursion tail-recursive: a local helper loop with an extra accumulator parameter, acc, carrying the answer so far. The helper is defined inside digitSum, so nobody else can call it with a wrong starting accumulator.

In everyday Scala you write explicit recursion less often than you might expect, because collection methods such as map, foldLeft and sum already capture the common patterns. Recursion shines on recursive data, such as the trees you will build in lesson 4 and in the final project.

Worked examplesH

Functions as values

scala
@main def functions(): Unit =
  val double: Int => Int = x => x * 2
  val add = (a: Int, b: Int) => a + b
  val square: Int => Int = n => n * n
  println(double(21))
  println(add(2, 3))

  def applyTwice(f: Int => Int, x: Int): Int = f(f(x))
  println(applyTwice(double, 5))
  println(applyTwice(_ + 10, 5))

  val incThenSquare = ((n: Int) => n + 1).andThen(square)
  println(incThenSquare(4))
  println(double.compose(square)(3))

  def multiplier(factor: Int): Int => Int = n => n * factor
  val triple = multiplier(3)
  println(triple(7))

  val nums = List(1, 2, 3, 4, 5, 6)
  println(nums.map(double))
  println(nums.filter(_ % 2 == 0))
  println(nums.map(n => n * n).sum)
  println(nums.reduce(_ + _))
text
42
5
20
25
25
18
21
List(2, 4, 6, 8, 10, 12)
List(2, 4, 6)
91
21

Step by step:

  • val double: Int => Int = x => x * 2 declares a function value. Because the type Int => Int is written on the left, the parameter x needs no annotation. In add, nothing tells the compiler the parameter types, so they are written inside the lambda.
  • applyTwice is a higher-order method: its first parameter has a function type. You can pass a named function (double) or write one on the spot.
  • _ + 10 is placeholder syntax: each _ stands for the next parameter, so _ + 10 means x => x + 10 and _ + _ means (a, b) => a + b. It is pleasant for very short functions; once a function needs a name for its parameter twice or spans more than a few symbols, write the parameter out.
  • f.andThen(g) runs f first, then g. f.compose(g) runs g first. So double.compose(square)(3) squares 3 and then doubles: 18.
  • multiplier(3) returns a function. That function is a closure: it refers to factor, a parameter of multiplier, and keeps it after multiplier has returned. triple(7) is 21.
  • map, filter and reduce take functions; collections are where you will pass functions all day.

Methods become functions automatically, and code can be passed by name

scala
def isPrime(n: Int): Boolean =
  n >= 2 && (2 to math.sqrt(n.toDouble).toInt).forall(d => n % d != 0)

def repeat(times: Int)(body: => Unit): Unit =
  for _ <- 1 to times do body

def orElse(first: String, fallback: => String): String =
  if first.nonEmpty then first else fallback

@main def byName(): Unit =
  println((1 to 30).filter(isPrime))

  var round = 0
  repeat(3) {
    round += 1
    println(s"round $round")
  }

  def slowDefault(): String =
    println("  (computing the default...)")
    "anonymous"
  println(orElse("Leilani", slowDefault()))
  println(orElse("", slowDefault()))
text
Vector(2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
round 1
round 2
round 3
Leilani
  (computing the default...)
anonymous
  • filter(isPrime) passes a method where a function is expected. The compiler wraps it in a function value for you; this is called eta-expansion, and in Scala 3 it is automatic.
  • body: => Unit is a by-name parameter. The argument is not evaluated when repeat is called; instead, it is evaluated every time repeat uses body. Here it runs three times. Combined with a second parameter list, this lets you call repeat(3) { ... } with a block, so your own method looks like a built-in control structure. In Scala 3 you may also write repeat(3): followed by an indented block.
  • orElse takes its fallback by name, so slowDefault() only runs when it is needed. In the first call it never runs at all. Logging libraries use exactly this trick so that building a debug message costs nothing when debug logging is off.
ExplanationH

What a function really is, and how professionals write them

On the JVM, a Scala function value is an object. The type Int => Int is shorthand for the trait Function1[Int, Int], which has one abstract method, apply. When you write double(21), Scala calls double.apply(21). That is why functions can be stored, passed and returned like any other object, and why methods such as andThen and compose exist on them: they are methods of Function1. The JVM compiles lambdas efficiently, so there is no reason to avoid them for speed in ordinary code.

A method, by contrast, is not an object; it is compiled to a JVM method. When a function value is needed, eta-expansion builds a small function object that calls the method. You can see why both exist: methods are what the JVM runs fastest and what classes are made of; function values are what make higher-order programming possible.

A few habits are shared by most Scala teams:

  • Write the result type of every public method. Inference is excellent, but an explicit type is documentation that the compiler checks, and it stops a change deep inside a method from silently changing its signature.
  • Keep functions small and pure where you can. Push printing, file access and network calls to the edges of the program and keep the logic in the middle pure. Pure functions can be tested with a single assert.
  • Do not use return. A method returns the value of its last expression. Scala has a return keyword, but inside a lambda it does something surprising (it tries to return from the enclosing method), and idiomatic code simply never needs it.
  • Prefer the standard combinators to hand-written recursion and loops, and reach for @tailrec when you do recurse over something large.

Analogy, not a formal construction

A by-name parameter is like handing someone a recipe instead of a cooked dish. A normal (by-value) argument is cooked before it is handed over, even if it is never eaten. A by-name argument is cooked by the receiver, only when needed, and freshly each time it is needed.

Common misconceptionsH

Common misconceptions

Misconception

"A by-name parameter is evaluated once, the first time it is used." It is evaluated every time it is used. If you need it at most once, copy it into a local lazy val (evaluated once, on first use) or a plain val.

Misconception

"_ + _ means 'add a value to itself'." Each underscore is a different parameter, so _ + _ is a two-argument function, (a, b) => a + b. To double a number, write x => x + x or _ * 2.

Misconception

"Recursion is always dangerous on the JVM." Non-tail recursion uses one stack frame per level, and the default stack holds thousands of frames, so recursion over a tree a few dozen levels deep is perfectly safe. The danger is recursion whose depth grows with the size of the input, such as walking a long list one element per call. Make that tail-recursive with @tailrec, or use a collection method.

Misconception

"Methods and functions are the same thing." They behave the same at the call site, and Scala converts between them, but a method is a member compiled to a JVM method, while a function value is an object with an apply method. Only function values can be stored in a collection or returned as a result.

Exercises

Exercises

Work out each answer on paper first. Then run the code to check yourself.

1. Functions and methods

Warm-upComputational

What does this program print? Write the two lines separated by a single space.

@main def ex(): Unit =
  def label(item: String, qty: Int = 1, unit: String = "kg"): String =
    s"$qty $unit $item"
  println(label("rice"))
  println(label("milk", unit = "L", qty = 2))

2. Functions and methods

Warm-upComputational

What does applyTwice(_ * 3, 2) return, given def applyTwice(f: Int => Int, x: Int): Int = f(f(x))?

3. Functions and methods

CoreComputational

What does this print? Write the two numbers separated by a space.

val f = (x: Int) => x + 1
val g = (x: Int) => x * 10
println(f.andThen(g)(2))
println(f.compose(g)(2))

4. Functions and methods

CoreComputational

What does this program print? Write the two lines separated by a space.

@main def ex(): Unit =
  var calls = 0
  def next(): Int =
    calls += 1
    calls
  def twice(x: => Int): Int = x + x
  println(twice(next()))
  println(calls)

5. Functions and methods

CoreComputational

What does this program print?

def counterFrom(start: Int): () => Int =
  var n = start
  () =>
    n += 1
    n

@main def ex(): Unit =
  val c = counterFrom(10)
  c()
  c()
  println(c())

6. Functions and methods

CoreHuman

What does the @tailrec annotation do?

Choose one

7. Functions and methods

StretchHuman

What is the type of (a: Int, b: String) => b * a? (On strings, * repeats: "ab" * 3 is "ababab".)

Choose one

Further reading and resources