100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

Lambda Expressions in Kotlin

Understand how to write and use lambda expressions in Kotlin, including trailing lambda syntax and the implicit it parameter.

Functions & LambdasIntermediate8 min readJul 8, 2026
Analogies

Introduction

A lambda expression is a small, anonymous block of code that can be treated as a value: passed to functions, stored in variables, or returned from other functions. Lambdas are central to Kotlin's functional programming style and are used extensively with collection operations like filter, map, and forEach. Unlike a named function, a lambda has no name and is typically defined inline where it's needed.

🏏

Cricket analogy: Like a franchise signing a substitute fielder for just one over who isn't on the permanent squad list, a lambda is an anonymous, disposable block of logic passed in wherever it's needed rather than a named, permanently defined function.

Syntax

kotlin
val sum = { x: Int, y: Int -> x + y }

// Trailing lambda syntax when the lambda is the last parameter
val positives = list.filter { it > 0 }

// Explicit parameter type omitted, inferred from context
val doubled = list.map { n -> n * 2 }

Explanation

A lambda is written inside curly braces { }. Parameters are listed before the -> arrow, and the code after the arrow is the body, whose last expression is the returned value. When a lambda is the last (or only) parameter of a function call, Kotlin allows it to be written outside the parentheses, or the parentheses to be omitted entirely — this is called trailing lambda syntax. If a lambda takes exactly one parameter, Kotlin lets you skip naming it and refer to it implicitly as it.

🏏

Cricket analogy: Like a scorer's shorthand where 'W' before the slash means wicket type and everything after is the outcome, a lambda's parameters go before the arrow and the body after it, and when it's the last argument, it's written outside the parentheses, like a trailing note on the scorecard.

Example

kotlin
fun main() {
    val numbers = listOf(1, -2, 3, -4, 5)

    val add: (Int, Int) -> Int = { a, b -> a + b }
    println(add(2, 3))

    val positives = numbers.filter { it > 0 }
    println(positives)

    val squares = numbers.map { it * it }
    println(squares)
}

Output

kotlin
5
[1, 3, 5]
[1, 4, 9, 16, 25]

Key Takeaways

  • Lambdas are anonymous blocks of code delimited by curly braces { }.
  • Parameters appear before the ->, and the body follows.
  • A single-parameter lambda can use the implicit name it instead of naming the parameter.
  • Trailing lambda syntax moves the lambda outside the parentheses when it's the last argument.
  • Lambdas can be stored in variables typed as function types, like (Int, Int) -> Int.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#LambdaExpressionsInKotlin#Lambda#Expressions#Syntax#Explanation#Functions#StudyNotes#SkillVeris