> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Lambda Expression

A lambda expression in Kotlin is an anonymous function literal defined within curly braces `{}` that can be treated as a first-class value. It is not declared with the `fun` keyword, but rather instantiated as an object of a function type, allowing it to be assigned to variables, passed as an argument, or returned from higher-order functions.

## Base Syntax

The full syntactic form of a lambda expression requires the parameters and the body to be enclosed within curly braces, separated by the `->` operator.

```kotlin theme={"dark"}
val sum: (Int, Int) -> Int = { a: Int, b: Int -> a + b }
```

* **(Int, Int) -> Int**: The explicit function type declaration.
* **`{ ... }`**: The lambda literal.
* **a: Int, b: Int**: The parameter list.
* **->**: The arrow operator separating parameters from the body.
* **a + b**: The lambda body.

## Type Inference

The Kotlin compiler can infer parameter types and return types based on the context. If the function type is explicitly declared on the variable, the parameter types inside the lambda literal can be omitted.

```kotlin theme={"dark"}
val sum: (Int, Int) -> Int = { a, b -> a + b }
```

## Implicit `it` Parameter

When a lambda expression has exactly one parameter, the compiler allows the omission of the parameter declaration and the `->` operator. The single parameter is implicitly exposed under the name `it`.

```kotlin theme={"dark"}
val square: (Int) -> Int = { it * it }
```

## Return Values

Lambda expressions do not use the `return` keyword for standard execution flow. The evaluated result of the last expression inside the lambda body is implicitly treated as the return value.

```kotlin theme={"dark"}
val calculateAndFormat: (Int, Int) -> String = { a, b ->
    val result = a + b
    "The result is $result" // Implicitly returned
}
```

To explicitly return from a lambda, a qualified return syntax (`return@label`) must be used, typically referencing the function to which the lambda was passed.

## Trailing Lambda Convention

If the last parameter of a function is a function type, a lambda expression passed as the corresponding argument can be placed outside the function's parentheses.

```kotlin theme={"dark"}
// Function signature: fun executeOperation(x: Int, operation: (Int) -> Unit)
executeOperation(10) { value ->
    println(value)
}
```

If the lambda is the *only* argument being passed to the function, the parentheses can be omitted entirely.

```kotlin theme={"dark"}
// Function signature: fun executeOperation(operation: () -> Unit)
executeOperation {
    println("Executing")
}
```

## Unused Parameters

If a lambda has multiple parameters but not all are used within the body, the unused parameters can be replaced with an underscore `_` to avoid compiler warnings and signal intent.

```kotlin theme={"dark"}
val processMap: (Int, String) -> String = { _, value ->
    "Processing $value"
}
```

## Closures

Kotlin lambdas support closures, meaning they can access and modify variables declared in the outer scope where the lambda is defined. Unlike Java, Kotlin allows the mutation of captured variables (they do not need to be `final` or effectively final).

```kotlin theme={"dark"}
var counter = 0
val increment: () -> Unit = {
    counter++ // Mutating a variable from the enclosing scope
}
```

## Destructuring in Lambdas

If a lambda parameter is a type that supports destructuring declarations (such as `Pair`, `Map.Entry`, or a `data class`), it can be destructured directly in the parameter list by wrapping the variables in parentheses.

```kotlin theme={"dark"}
val processPair: (Pair<Int, String>) -> Unit = { (id, name) ->
    println("$id: $name")
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
