Skip to main content
Browse topics

Lambda with Parameters in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

Lambda with Parameters 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.

Lambda parameters are declared before the arrow symbol ->. 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

  • Lambda parameters are declared before the arrow symbol ->.
  • Type can be inferred when context is clear.
  • Single parameter can use it keyword.

Syntax

kotlin
{ x: Int -> x * x }

Lambda with Parameters in Kotlin Example Program in Kotlin

kotlin
fun main(args: Array<String>) {
    val square: (Int) -> Int = { value -> value * value }
    println(square(5))
}

Sample Output

plaintext
25

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 square: (Int) -> Int = { value -> value * value } assigns or updates a value used later in the program.

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

  4. Lambda parameters are declared before the arrow symbol ->.

  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

Common Mistakes

Key Points

  • Lambda parameters are declared before the arrow symbol ->.
  • Type can be inferred when context is clear.
  • Single parameter can use it keyword.
  • 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 with Parameters in Kotlin?
Lambda parameters are declared before the arrow symbol ->.
When should I use Lambda with Parameters?
Use lambdas for short callbacks passed to collection operations, event handlers, or higher-order functions.
How is Lambda with Parameters different from Java?
Single parameter can use it keyword.
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