> ## 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 Return Expression

The `return` expression in Kotlin is a control flow construct that immediately terminates the execution of the innermost enclosing function or anonymous function, yielding a specified value to the caller. In Kotlin's type system, `return` is an expression that evaluates to the bottom type `Nothing`. This signifies that the expression never completes normally, allowing it to be used in contexts expecting any arbitrary type.

## Syntax

```kotlin theme={"dark"}
// Standard return
return [expression]

// Labeled return
return@[label] [expression]
```

## Evaluation and Type Mechanics

Because `return` evaluates to `Nothing`, it can be used as an operand in expressions (such as the Elvis operator) without causing type mismatches. The compiler statically determines that any code following a `return` expression in the same control flow branch is unreachable.

```kotlin theme={"dark"}
// The compiler allows assigning Nothing to a String type 
// because the assignment will never actually execute.
val result: String = nullableString ?: return
```

## Scope and Resolution

The execution target of a `return` expression depends strictly on its lexical context, the `fun` keyword, and the presence of labels.

### Unlabeled Returns

An unlabeled `return` always terminates the nearest enclosing function declared with the `fun` keyword.

```kotlin theme={"dark"}
fun standardFunction(): Int {
    return 42 // Terminates standardFunction
}
```

### Non-Local Returns

When an unlabeled `return` expression is used inside a lambda expression passed to an `inline` higher-order function, it performs a non-local return. It bypasses the lambda's scope and terminates the nearest enclosing `fun`.

```kotlin theme={"dark"}
inline fun inlineExecutor(block: () -> Unit) { block() }

fun enclosingFunction() {
    inlineExecutor {
        return // Terminates enclosingFunction, not just the lambda
    }
}
```

*Note: Unlabeled returns are syntactically prohibited inside lambdas passed to non-inline functions because the compiler cannot guarantee the call stack or execution context of the lambda.*

### Labeled Returns

To terminate a lambda expression without exiting the enclosing function, a labeled `return` is required. The label explicitly defines the execution scope to terminate, effectively acting as a local return for the lambda.

```kotlin theme={"dark"}
fun enclosingFunction() {
    listOf(1, 2, 3).forEach customLabel@{
        return@customLabel // Terminates only the lambda execution
    }
}
```

Kotlin provides implicit labels that match the name of the higher-order function to which the lambda is passed, eliminating the need for explicit label declarations.

```kotlin theme={"dark"}
fun enclosingFunction() {
    listOf(1, 2, 3).forEach {
        return@forEach // Implicit label targeting the forEach lambda
    }
}
```

### Anonymous Functions

Unlike lambdas, anonymous functions are explicitly declared using the `fun` keyword. Consequently, an unlabeled `return` inside an anonymous function resolves to the anonymous function itself, not the enclosing function.

```kotlin theme={"dark"}
fun enclosingFunction() {
    listOf(1, 2, 3).forEach(fun(value: Int) {
        return // Terminates only the anonymous function
    })
}
```

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