← Back to Kotlin Course | Chapter 3: Control Flow | Lesson 7 of 7

Return with Labels

A labeled return lets you jump out of just one specific inner block, like a lambda, instead of leaving the whole function.

Non-Local Return from a Lambda

A plain return inside a lambda passed to an inline function like forEach does not just skip that one iteration -- it exits the entire enclosing function immediately.

Example: Non-Local Return from a Lambda

markup
fun findFirstEven(numbers: List<Int>): Int? {
    numbers.forEach {
        if (it % 2 == 0) return it
    }
    return null
}

fun main() {
    println("First even: ${findFirstEven(listOf(1, 3, 4, 5))}")
}

Returning from the Lambda Only

Using the implicit label named after the function the lambda is passed to, such as return@forEach, returns from just that single lambda invocation and lets the loop continue with the next item.

Example: Returning from the Lambda Only

markup
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    numbers.forEach {
        if (it % 2 == 0) return@forEach
        println("Odd: $it")
    }
}

Explicit Labels on a Lambda

You can also define your own explicit label before a lambda, like myLoop@ { ... }, and return from it specifically using return@myLoop.

Example: Explicit Labels on a Lambda

markup
fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    numbers.forEach myLoop@{
        if (it == 3) return@myLoop
        println("Value: $it")
    }
}

Choosing Local vs Non-Local Return

Deciding whether a return inside a lambda should exit the whole function or just that lambda invocation is a deliberate choice -- use the labeled form whenever only the lambda should stop.

Example: Choosing Local vs Non-Local Return

markup
fun processAll(numbers: List<Int>) {
    for (n in numbers) {
        numbers.forEach {
            if (it == n) return@forEach
            println("Comparing $n with $it")
        }
    }
}

fun main() {
    processAll(listOf(1, 2))
}
Common Mistakes
  1. Assuming a plain return inside a lambda passed to forEach exits only that lambda, when it actually returns from the enclosing function (a non-local return).
  2. Forgetting to label the return when only the lambda itself should stop, resulting in the surrounding function exiting unexpectedly early.
  3. Using implicit labels (named after the function, like forEach$0) instead of the clearer explicit label syntax when precision matters.
Chapter Summary
  • A plain return inside a lambda passed to an inline function performs a non-local return, exiting the enclosing function entirely.
  • Labeling a lambda with name@ { ... } and using return@name returns from just that lambda, not the whole function.
  • Kotlin also provides an implicit label matching the name of the function the lambda is passed to, such as return@forEach.
  • Choosing the right kind of return (local vs non-local) avoids surprising control-flow bugs in loops built from higher-order functions.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.