> ## 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 Parameter

A lambda parameter in Kotlin is a variable declared within a lambda expression that receives an argument when the underlying function type is invoked. These parameters are defined on the left side of the arrow (`->`) operator, which separates the parameter list from the execution body.

## Explicit Parameter Declaration

By default, lambda parameters are declared explicitly with their names and types.

```kotlin theme={"dark"}
val multiplyExplicit: (Int, Int) -> Int = { x: Int, y: Int -> x * y }
```

If the Kotlin compiler can infer the parameter types from the surrounding context (such as the declared function type of the variable), the explicit type declarations can be omitted.

```kotlin theme={"dark"}
val multiplyInferred: (Int, Int) -> Int = { x, y -> x * y }
```

## The Implicit `it` Parameter

When a lambda expression has exactly one parameter, and its type can be inferred by the compiler, Kotlin allows you to omit the parameter declaration and the `->` operator entirely. The single parameter is implicitly exposed under the keyword `it`.

```kotlin theme={"dark"}
// Explicit declaration
val squareExplicit: (Int) -> Int = { number -> number * number }

// Implicit 'it' declaration
val squareImplicit: (Int) -> Int = { it * it }
```

## Unused Parameters

If a lambda requires multiple parameters to satisfy a function type signature, but the lambda body does not utilize all of them, the unused parameters can be replaced with an underscore (`_`). This prevents compiler warnings and explicitly signals to other developers that the parameter is intentionally ignored.

```kotlin theme={"dark"}
val logMessage: (Int, String, Throwable) -> Unit = { _, message, _ -> 
    println(message) 
}
```

## Destructuring Declarations

Lambda parameters support destructuring declarations. If a parameter is a type that provides `componentN()` operator functions (such as a `Data Class`, `Pair`, or `Map.Entry`), you can destructure the object directly within the parameter list by wrapping the destructured variables in parentheses.

```kotlin theme={"dark"}
// Standard parameter
val processPairStandard: (Pair<Int, String>) -> Unit = { pair ->
    println("${pair.first} and ${pair.second}")
}

// Destructured parameter
val processPairDestructured: (Pair<Int, String>) -> Unit = { (id, name) ->
    println("$id and $name")
}
```

You can also combine destructuring with the unused parameter underscore if you only need specific components of the destructured object:

```kotlin theme={"dark"}
val processPairPartial: (Pair<Int, String>) -> Unit = { (_, name) ->
    println("Only using $name")
}
```

## Default Values Restriction

Unlike standard function parameters, lambda parameters cannot have default values. The Kotlin compiler strictly enforces this rule, meaning all arguments must be provided when the lambda's underlying function type is invoked. Attempting to assign a default value to a lambda parameter results in a syntax error.

```kotlin theme={"dark"}
// INVALID SYNTAX: Compiler error "Lambda parameters cannot have default values"
// val increment: (Int) -> Int = { x: Int = 1 -> x + 1 }
```

## Shadowing

Lambda parameters follow standard lexical scoping rules. If a lambda parameter shares a name with a variable in the enclosing scope, the lambda parameter will shadow the outer variable within the lambda body.

```kotlin theme={"dark"}
val x = 10
val addToX: (Int) -> Int = { x -> 
    x + 5 // 'x' refers to the lambda parameter, not the outer variable
}
```

<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>
