0Pricing
Kotlin Academy · Lesson

Passing Lambdas to Functions

Pass lambdas as arguments and use trailing lambda syntax.

Passing Lambdas to Functions is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Trailing Lambda Syntax

If the last parameter is a function, you can move the lambda outside the parentheses. This is 'trailing lambda' syntax.
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))

// Standard:
applyTwice(3, { it * 2 })
// Trailing lambda:
applyTwice(3) { it * 2 }

Trailing Lambda Without Parens

When the lambda is the ONLY argument, you can omit the parentheses entirely.
fun execute(block: () -> Unit) = block()

// With parens:
execute({ println("Hello") })
// Without parens (idiomatic):
execute { println("Hello") }

Passing a Lambda Variable

You can pass a stored lambda reference using its variable name.
val double = { n: Int -> n * 2 }
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))
println(applyTwice(5, double))  // 20

Function References with ::

Use :: to pass a named function as a lambda-compatible argument.
fun triple(n: Int) = n * 3
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))
println(applyTwice(2, ::triple))  // 18

Multiple Lambda Parameters

A function can accept multiple lambda parameters. Only the last one can use trailing lambda syntax.
fun transform(
    value: Int,
    before: (Int) -> Int,
    after: (Int) -> Int
) = after(before(value))

println(transform(5, { it * 2 }, { it + 1 }))  // 11

Passing a Lambda to filter

filter accepts a predicate lambda — a function from element to Boolean.
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evens = numbers.filter { it % 2 == 0 }
println(evens)  // [2, 4, 6]

Chaining Higher-Order Functions

Chain multiple higher-order function calls for expressive data pipelines.
val result = (1..10)
    .filter { it % 2 != 0 }  // odds
    .map { it * it }          // square
    .take(3)                  // first 3
println(result)  // [1, 9, 25]

Lambda Capturing Variables

Lambdas can capture variables from the surrounding scope (closure). In Kotlin, they can even capture and modify var.
var total = 0
listOf(1, 2, 3, 4).forEach { total += it }
println(total)  // 10

Returning a Lambda from a Function

Higher-order functions can also return lambdas, creating factory functions.
fun multiplierOf(factor: Int): (Int) -> Int {
    return { n -> n * factor }
}
val double = multiplierOf(2)
val triple = multiplierOf(3)
println(double(5))  // 10
println(triple(5))  // 15

Quick Check

When can you move a lambda outside the parentheses using trailing lambda syntax?

Recap

Higher-order functions take lambdas as parameters. Trailing lambda syntax moves the lambda outside parens when it's last. :: passes named function references. Next: the implicit 'it' parameter!

Frequently asked questions

Is the “Passing Lambdas to Functions” lesson free?

Yes — the full text of “Passing Lambdas to Functions” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Passing Lambdas to Functions”?

Pass lambdas as arguments and use trailing lambda syntax. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Passing Lambdas to Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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