Functions as Values
First-class functions.
Functions as Values is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Functions are values
In Scala, functions are first-class values. You can store them in variables, pass them as arguments, and return them from other functions, just like numbers or strings.
object Main {
def main(args: Array[String]): Unit = {
val increment: Int => Int = x => x + 1
println(increment(5))
}
}Function type syntax
The type Int => String means a function taking an Int and returning a String. Multiple parameters use parentheses: (Int, Int) => Int.
object Main {
def main(args: Array[String]): Unit = {
val add: (Int, Int) => Int = (a, b) => a + b
val label: Int => String = n => s"value=$n"
println(add(3, 4))
println(label(7))
}
}Lambda (anonymous function) syntax
An anonymous function, or lambda, is written (params) => body. It has no name and is often passed directly to another function.
object Main {
def main(args: Array[String]): Unit = {
val square = (x: Int) => x * x
println(square(6))
}
}Higher-order functions
A higher-order function is one that takes a function as a parameter or returns a function. map and filter are classic examples.
object Main {
def applyTwice(f: Int => Int, x: Int): Int = f(f(x))
def main(args: Array[String]): Unit = {
println(applyTwice(_ + 3, 10))
}
}Passing functions to methods
Collection methods accept functions. You can pass a lambda inline or pass a named function value.
object Main {
def main(args: Array[String]): Unit = {
val triple = (x: Int) => x * 3
println(List(1, 2, 3).map(triple))
println(List(1, 2, 3).map(x => x * 3))
}
}Returning functions
A function can return another function. Here multiplier returns a function that multiplies by a fixed factor, a small function factory.
object Main {
def multiplier(factor: Int): Int => Int = x => x * factor
def main(args: Array[String]): Unit = {
val timesTen = multiplier(10)
println(timesTen(5))
}
}Methods vs functions
A def defines a method. You can turn a method into a function value with the eta-expansion underscore: methodName _, or just by using it where a function is expected.
object Main {
def doubleIt(x: Int): Int = x * 2
def main(args: Array[String]): Unit = {
val f: Int => Int = doubleIt
println(f(8))
println(List(1, 2, 3).map(doubleIt))
}
}Functions with no parameters
A function can take no arguments. Its type is written () => T. This is useful for deferring computation.
object Main {
def main(args: Array[String]): Unit = {
val greeting: () => String = () => "Hello!"
println(greeting())
}
}Storing functions in collections
Because functions are values, you can store them in lists or maps and look them up dynamically, building a tiny command dispatcher.
object Main {
def main(args: Array[String]): Unit = {
val ops: Map[String, (Int, Int) => Int] = Map(
"add" -> (_ + _),
"mul" -> (_ * _)
)
println(ops("add")(3, 4))
println(ops("mul")(3, 4))
}
}Functions as configuration
Passing a function lets callers customize behavior. Here a generic transformAll applies any function across a list.
object Main {
def transformAll(xs: List[Int], f: Int => Int): List[Int] = xs.map(f)
def main(args: Array[String]): Unit = {
println(transformAll(List(1, 2, 3), _ + 100))
println(transformAll(List(1, 2, 3), _ * 10))
}
}A cleaner customization example
Here is the same idea written cleanly: callers supply the transformation, keeping the helper generic and reusable.
object Main {
def transformAll(xs: List[Int], f: Int => Int): List[Int] = xs.map(f)
def main(args: Array[String]): Unit = {
println(transformAll(List(1, 2, 3), x => x + 100))
println(transformAll(List(1, 2, 3), x => x * x))
}
}Quick Check
What is a higher-order function?
Recap
You learned that functions are first-class values:
- Store functions in
vals, lists, and maps - Function types like
Int => String - Lambdas:
(x) => bodyand the_shorthand - Higher-order functions take or return functions
- Methods become function values via eta-expansion
Frequently asked questions
Is the “Functions as Values” lesson free?
Yes — the full text of “Functions as Values” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “Functions as Values”?
First-class functions. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Functions as Values” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Functions as Values
- Currying
- Composition
- Closures