KotlinFunctionsIntermediate

Lambda Expressions

Anonymous functions that can be passed as arguments, stored in variables, or returned from other functions.

Review the syntaxStudy the examplesOpen the coding app
{ param1: Type1, param2: Type2 -> body_expression }
{ it -> body_expression } // For single parameter
{ body_expression } // For no parameters

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Anonymous functions that can be passed as arguments, stored in variables, or returned from other functions.

Lambda expressions in Kotlin are a concise way to define function literals, which are functions that are not declared but passed immediately as an expression. They are fundamental to functional programming paradigms and are widely used in Kotlin's standard library for higher-order functions like `map`, `filter`, and `forEach`. The basic syntax involves enclosing the function body in curly braces `{}`. Parameters are declared before the `->` symbol, and the body follows. If a lambda has only one parameter, its name can be omitted, and it can be implicitly referred to as `it`. If a lambda has no parameters, the `->` can also be omitted. Lambdas can capture variables from their enclosing scope, forming a closure, which makes them very powerful for creating context-aware behavior. Kotlin also supports a 'trailing lambda' syntax, where if the last parameter of a function is a lambda, it can be moved outside the parentheses, significantly improving readability for DSLs and builders. Understanding lambdas is key to writing idiomatic and expressive Kotlin code, especially when working with collections and asynchronous operations.

Quick reference

Syntax

{ param1: Type1, param2: Type2 -> body_expression }
{ it -> body_expression } // For single parameter
{ body_expression } // For no parameters

See it in practice

Examples

1

Simple lambda with explicit parameters

val sum: (Int, Int) -> Int = { a, b -> a + b }
val result = sum(5, 3)
println("Sum: $result")
Output:
Sum: 8

A lambda expression assigned to a variable `sum` that takes two integers and returns their sum. Parameter types can often be inferred.

2

Lambda with implicit 'it' parameter

val numbers = listOf(1, 2, 3, 4)
val doubled = numbers.map { it * 2 }
println("Doubled: $doubled")
Output:
Doubled: [2, 4, 6, 8]

When a lambda has a single parameter, it can be implicitly named `it`, making the syntax even more concise.

3

Trailing lambda syntax

fun operateOnList(list: List<Int>, operation: (Int) -> Int): List<Int> {
    return list.map(operation)
}

val original = listOf(1, 2, 3)
val processed = operateOnList(original) { num ->
    num * num
}
println("Processed: $processed")
Output:
Processed: [1, 4, 9]

If a lambda is the last argument to a function, it can be moved outside the parentheses, improving readability, especially for multi-line lambdas.

Debug faster

Common Errors

1

Type Mismatch

Cause: Providing a lambda that doesn't match the expected function type signature (number of parameters, their types, or return type).

Fix: Ensure the lambda's parameters and return type align with the function type expected by the higher-order function or variable assignment. Explicitly declare parameter types within the lambda if inference fails.

val process: (String) -> Int = { s: String, i: Int -> s.length + i } // Error: Too many parameters for (String) -> Int

Runtime support

Compatibility

JVMN/AKotlin 1.0+

Source: Kotlin Documentation

Common questions

Frequently Asked Questions

Anonymous functions that can be passed as arguments, stored in variables, or returned from other functions.

Type Mismatch: Ensure the lambda's parameters and return type align with the function type expected by the higher-order function or variable assignment. Explicitly declare parameter types within the lambda if inference fails.