What you'll learn
This module opens the door to functional Kotlin. Lambdas let you treat behaviour as data — storing a piece of logic in a variable and passing it around — and higher-order functions take or return functions. Together they power Kotlin's elegant collection operations (next module).
By the end you'll be able to:
- Write lambda expressions
- Use the implicit
itparameter - Write and call higher-order functions
- Use the trailing-lambda convention and function references
Lambda expressions
A lambda is an anonymous function written between curly braces: parameters, an arrow ->, then the body. The last expression in the body is its return value. You can store one in a variable and call it like any function:
{ n: Int -> n * n }Parameters
The inputs, before the arrow.
The arrow ->
Separates params from body.
Body
The last line is the return value.
val square = { n: Int -> n * n }
println(square(5)) // 25
val add = { a: Int, b: Int -> a + b }
println(add(3, 4)) // 7The implicit it
When a lambda takes a single parameter, Kotlin lets you skip naming it and use the implicit it instead. This makes short lambdas wonderfully concise — you'll see it constantly:
val numbers = listOf(1, 2, 3, 4, 5)
// when a lambda has ONE parameter, use the implicit 'it'
val doubled = numbers.map { it * 2 }
println(doubled) // [2, 4, 6, 8, 10]
val big = numbers.filter { it > 3 }
println(big) // [4, 5]Tip
it for short, obvious lambdas. If the lambda is longer or nested, name the parameter ({ number -> ... }) for clarity — future readers (including you) will thank you.Higher-order functions
A higher-order function takes a function as a parameter or returns one. The parameter's type is a function type like (Int) -> Unit — read as "takes an Int, returns nothing." Kotlin has a lovely convention here: if the last argument is a lambda, you write it outside the parentheses:
// a function that TAKES a function as a parameter
fun repeatAction(times: Int, action: (Int) -> Unit) {
for (i in 1..times) action(i)
}
// trailing-lambda convention: a final lambda goes OUTSIDE the parentheses
repeatAction(3) { i ->
println("Step $i")
}The trailing-lambda convention
repeatAction(3) { ... }, list.forEach { ... }, button.setOnClickListener { ... }. It's just a function whose last argument is a lambda, pulled outside the parentheses.Function types & references
You can store a function in a variable by giving it a function type, and you can point to an existing function with a reference using :: — handy when a lambda would just call one function anyway:
// a function stored in a variable, typed as (Int) -> Boolean
val isEven: (Int) -> Boolean = { it % 2 == 0 }
println(isEven(4)) // true
// a function REFERENCE with ::
fun triple(n: Int) = n * 3
val nums = listOf(1, 2, 3)
println(nums.map(::triple)) // [3, 6, 9]Recap & quick check
Key takeaways
- A lambda is { params -> body }; its last expression is the return value.
- For a single-parameter lambda, use the implicit 'it' instead of naming it.
- Higher-order functions take or return functions; parameter types look like (Int) -> Unit.
- The trailing-lambda convention puts a final lambda outside the parentheses — this powers Kotlin's clean APIs.
- Function references (::name) point to an existing function; a lambda can call it instead.
Quick check
1. What is a lambda in Kotlin?
2. When can you use the implicit 'it' in a lambda?
3. What is a higher-order function?
4. What does the trailing-lambda convention allow?
5. What does ::triple represent?
Excellent — you're thinking functionally now. Next up: Module 9 — Collection Operations, where lambdas truly shine.