Intermediate 8 min readKotlin 2.0

Kotlin Lambdas — Anonymous Functions Explained

A lambda is a function literal — a function without a name that can be stored in a variable or passed as a parameter.

What You Will Learn

  • Lambda syntax
  • The it implicit parameter
  • Store lambdas in variables
  • Pass lambdas to map, filter, forEach
  • Capture variables from outer scope

Lambda Syntax

A lambda is wrapped in { }. Parameters are listed before ->. The last expression is the return value.

Basic Lambda

kotlin
fun main() {
    val double: (Int) -> Int = { n -> n * 2 }
    val shout: (String) -> String = { s -> s.uppercase() + "!" }

    println(double(5))
    println(shout("kotlin"))

    val sum: (Int, Int) -> Int = { a, b -> a + b }
    println(sum(3, 7))
}
Output
10 KOTLIN! 10

(Int) -> Int is the function type. { n -> n * 2 } is the lambda. n is the parameter, n * 2 is the body and implicit return value.

Beginner Tip: When a lambda has exactly one parameter, you can omit it and use "it" as the implicit name.

The "it" Parameter

When a lambda has a single parameter, you can skip the parameter declaration and use "it".

Using it

kotlin
fun main() {
    val double: (Int) -> Int = { it * 2 }
    val nums = listOf(1, 2, 3, 4, 5)
    println(nums.map { it * 3 })
    println(nums.filter { it % 2 == 0 })
    println(nums.forEach { println(it) })
}
Output
[3, 6, 9, 12, 15] [2, 4] 1 2 3 4 5 null

"it" refers to the single lambda parameter. This is idiomatic Kotlin — use it for short, clear lambdas.

Lambdas with Collections

filter, map, reduce, and forEach all take lambdas.

Collection Operations with Lambdas

kotlin
fun main() {
    val names = listOf("Alice","Bob","Carol","Dave")

    val long = names.filter { it.length > 3 }
    val upper = names.map { it.uppercase() }
    val joined = names.joinToString(", ")
    val total = listOf(1,2,3,4,5).reduce { acc, n -> acc + n }

    println(long)
    println(upper)
    println(joined)
    println(total)
}
Output
[Alice, Carol, Dave] [ALICE, BOB, CAROL, DAVE] Alice, Bob, Carol, Dave 15

filter keeps elements where the lambda returns true. map transforms each element. reduce accumulates a single result.

Closures — Capturing Variables

Lambdas can access and modify variables from their enclosing scope.

Closure

kotlin
fun main() {
    var count = 0
    val increment = { count++ }
    increment()
    increment()
    increment()
    println(count)
}
Output
3

The lambda captures the count variable from the outer scope and increments it each time it is called.

Best Practice: Be careful capturing mutable variables in lambdas — it can lead to unexpected behaviour when lambdas are called at a later time.

Practice Exercise

Exercisepredict output

What prints? val nums = listOf(1,2,3,4,5) println(nums.filter { it > 3 }.map { it * 10 })

Quick Quiz

Quick Quiz

What is "it" in a Kotlin lambda?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review