0Pricing
Kotlin Academy · Lesson

Passing Lambdas to Functions

Pass lambdas as arguments and use trailing lambda syntax.

Functions That Accept Lambdas

A higher-order function is one that takes a function (like a lambda) as a parameter. This is how map, filter, and forEach work.

Declaring a Higher-Order Function

Use a function type as the parameter type.
fun applyTwice(n: Int, op: (Int) -> Int): Int {
    return op(op(n))
}
println(applyTwice(3, { it * 2 }))  // 12

All lessons in this course

  1. What Is a Lambda? Syntax and Types
  2. Passing Lambdas to Functions
  3. it: The Implicit Parameter
  4. filter, map, and find on Collections
← Back to Kotlin Academy