Language Elements

Data Types

Kotlin Classes

Kotlin Operators

Kotlin Constructors

Kotlin Null Safety

Extension Functions

Lambda Functions

Object Oriented Kotlin

Data Classes

Coroutines

Kotlin Collections

Kotlin Data Structures

Kotlin Algorithms

Delegation

Lateinit and Lazy Initialization

Kotlin Scope Functions

Kotlin Key Words

Kotlin Example Codes

Kotlin Interview Questions

Kotlin Lambda Functions

Lambda functions in Kotlin are unnamed blocks of executable code (anonymous) that combine the power of functions with the flexibility of variables. They allow behavior to be passed as arguments or stored in variables, enabling concise and functional programming patterns. While they may not have descriptive names like regular functions, lambdas are considered expressive because they communicate the logic directly at the point of use, making code more concise and readable. This is especially useful in operations like filtering, mapping, or sorting collections.

Key Features of Kotlin Lambda Functions

When Not to Use Kotlin Lambda Functions

While lambda functions are the most commonly used anonymous functions in Kotlin, they are not the only form—Kotlin also supports anonymous functions declared with the fun keyword. Both allow functions to be treated like values and can be stored in variables, passed as arguments, or returned from other functions. Lambdas are well-suited for short, localized logic, such as collection transformations or UI callbacks. However, for logic that is complex, recursive, or used in multiple places, named functions provide better structure, readability, and reusability. There is no need to use Lambda functions in these cases as it can reduce code clarity and problematic for debugging.

Basic Syntax

The basic syntax of a Kotlin lambda expression is:

{ parameters -> body }

{ and } define the lambda function block.
-> separates the parameter list from the executable body.
body is the logic that gets executed when the lambda is invoked.

The parameters represent the input values, and the body defines the operations to be performed using those values. If there is only one parameter, Kotlin allows you to refer to it implicitly using the keyword it, and omit the parameter declaration entirely.

Kotlin Lambda Function Examples

Expressing Lambdas as Expression

    
val add: (Int, Int) -> Int = { a, b -> a + b }
    
val sum = { a: Int, b: Int -> a + b }
    
val printMessage: (String) -> Unit = { message -> println(message)}

val multiply = { a, b -> a * b } // Compiler infers parameter types
    
Using Lambda Functions
    
val add: (Int, Int) -> Int = { a, b -> a + b }
val result = add(3, 4) // result is now 7

// Inline usage without storing in variables
val numbers = listOf(1, 2, 3, 4, 5)

val squaredNumbers = numbers.map {number -> number * number }

val sum = numbers.reduce { acc, num -> acc + num }

Common Types of Kotlin Lambda Functions

1. Stateless Lambdas

Pure functions with no external dependencies

val square = { x: Int -> x * x }

2. Stateful Lambdas

Capture variables from their enclosing scope

var counter = 0
val incrementer = { counter++ }

3. Predicate Lambdas

Return boolean values for filtering

val isEven = { n: Int -> n % 2 == 0 }
numbers.filter(isEven)

4. Transformer Lambdas

Convert input to different output

val toUpperCase = { s: String -> s.uppercase() }
names.map(toUpperCase)

5. Consumer Lambdas

Accept input for side effects

val printer = { item: Any -> println(item) }
list.forEach(printer)

6. Supplier Lambdas

Generate values without inputs

val randomizer = { Random.nextInt() }

7. Multi-Parameter Lambdas

Handle multiple arguments

val sum = { a: Int, b: Int -> a + b }

8. Receiver Lambdas

Work with implicit receiver objects

val greet: String.() -> Unit = { println("Hello, $this!") }
"Kotlin".greet()

9. Suspending Lambdas

Support coroutine operations

val fetchData: suspend () -> String = { 
    delay(1000)
    "Data loaded"
}


Copyright © by Zafar Yasin. All rights reserved.