0Pricing
Android Academy · Lesson

Lambdas and Higher-Order Functions

Pass behavior as values.

What Is a Lambda?

A lambda is a small function you can write inline, without giving it a name.

You wrap it in curly braces: { x -> x + 1 }. Lambdas let you pass behavior around like data.

You already used them with map and filter.

Storing a Lambda in a Variable

A lambda is a value, so you can store it in a variable and call it later.

Call it like a function using parentheses.

fun main() {
    val square = { x: Int -> x * x }
    println(square(5))
    println(square(3))
}

All lessons in this course

  1. Lists and Mutable Lists
  2. Maps and Sets
  3. map, filter, forEach
  4. Lambdas and Higher-Order Functions
← Back to Android Academy