Skip to main content

Lambda as Parameter in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

Lambda as Parameter is a fundamental concept every Kotlin developer should understand. Lambda expressions are anonymous functions you can pass as values — the foundation of functional-style APIs in Kotlin.

Functions can accept lambda as a parameter. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Functions can accept lambda as a parameter.
  • Caller can pass logic inline using trailing lambda syntax.
  • This enables flexible and reusable code.

Syntax

fun operate(a: Int, b: Int, action: (Int, Int) -> Int)

Lambda as Parameter in Kotlin Example Program in Kotlin

fun operate(a: Int, b: Int, action: (Int, Int) -> Int): Int {
    return action(a, b)
}

fun main(args: Array<String>) {
    val result = operate(8, 2) { x, y -> x - y }
    println(result)
}

Sample Output

6

When to use

Use lambdas for short callbacks passed to collection operations, event handlers, or higher-order functions.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val result = operate(8, 2) { x, y -> x - y } assigns or updates a value used later in the program.

  3. The println(result) statement writes a line to the console — this produces part of the sample output below.

  4. Functions can accept lambda as a parameter.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Keep lambdas short; extract longer logic into named functions.
  • Use it only when the lambda has a single parameter and the meaning is obvious.
  • Prefer trailing lambda syntax when the last argument is a function.

Common Mistakes

  • Capturing mutable variables unsafely in concurrent lambdas.
  • Over-nesting lambdas when a named function would be clearer.
  • Forgetting that return inside a lambda returns from the lambda, not the outer function (unless inline).

Key Points

  • Functions can accept lambda as a parameter.
  • Caller can pass logic inline using trailing lambda syntax.
  • This enables flexible and reusable code.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Lambda as Parameter in Kotlin?
Functions can accept lambda as a parameter.
When should I use Lambda as Parameter?
Use lambdas for short callbacks passed to collection operations, event handlers, or higher-order functions.
How is Lambda as Parameter different from Java?
This enables flexible and reusable code.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials