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

Labels के साथ Return

एक labeled return आपको पूरे function से निकलने के बजाय सिर्फ एक specific inner block, जैसे lambda, से बाहर jump करने देता है।
Syntax
kotlin
collection.forEach {
    return@forEach          // return from the lambda only
}

collection.forEach label@{
    return@label
}

Non-Local Return from a Lambda

forEach जैसे किसी inline function को पास किए lambda के अंदर एक साधारण return सिर्फ उस एक iteration को skip नहीं करता -- यह तुरंत पूरे enclosing function से बाहर निकल जाता है।

उदाहरण: Non-Local Return from a Lambda

markup
// Define the function `findFirstEven` taking `numbers` and returning a `Int?`
fun findFirstEven(numbers: List<Int>): Int? {
    numbers.forEach {
        if (it % 2 == 0) return it
    }
    // Return `null` from this function
    return null
}

// Entry point: execution of the program starts here
fun main() {
    // Print "First even: ${findFirstEven(listOf(1, 3, 4, 5))}" to the console, with a trailing newline
    println("First even: ${findFirstEven(listOf(1, 3, 4, 5))}")
}

Returning from the Lambda Only

जिस function को lambda पास किया गया है उसके नाम पर बने implicit label का उपयोग करना, जैसे return@forEach, सिर्फ उस single lambda invocation से return करता है और loop को अगले item के साथ जारी रहने देता है।

उदाहरण: Returning from the Lambda Only

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5)`
    val numbers = listOf(1, 2, 3, 4, 5)
    numbers.forEach {
        if (it % 2 == 0) return@forEach
        // Print "Odd: $it" to the console, with a trailing newline
        println("Odd: $it")
    }
}

Explicit Labels on a Lambda

आप किसी lambda से पहले अपना खुद का explicit label भी define कर सकते हैं, जैसे myLoop@ { ... }, और specifically return@myLoop का उपयोग करके उससे return कर सकते हैं।

उदाहरण: Explicit Labels on a Lambda

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5)`
    val numbers = listOf(1, 2, 3, 4, 5)
    numbers.forEach myLoop@{
        if (it == 3) return@myLoop
        // Print "Value: $it" to the console, with a trailing newline
        println("Value: $it")
    }
}

Choosing Local vs Non-Local Return

यह तय करना कि किसी lambda के अंदर return को पूरे function से बाहर निकलना चाहिए या सिर्फ उस lambda invocation से, एक सोचा-समझा choice है -- जब भी सिर्फ lambda को रुकना चाहिए तब labeled form का उपयोग करें।

उदाहरण: Choosing Local vs Non-Local Return

markup
// Define the function `processAll` taking `numbers`
fun processAll(numbers: List<Int>) {
    // Loop over `numbers`, binding each item to `n`
    for (n in numbers) {
        numbers.forEach {
            if (it == n) return@forEach
            // Print "Comparing $n with $it" to the console, with a trailing newline
            println("Comparing $n with $it")
        }
    }
}

// Entry point: execution of the program starts here
fun main() {
    processAll(listOf(1, 2))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि forEach को पास किए किसी lambda के अंदर एक साधारण return सिर्फ उस lambda से बाहर निकलता है, जबकि यह असल में enclosing function से return करता है (एक non-local return)।
  2. जब सिर्फ lambda को ही रुकना चाहिए तब return को label करना भूल जाना, जिससे surrounding function अनपेक्षित रूप से जल्दी exit हो जाता है।
  3. जब precision मायने रखे तब ज़्यादा स्पष्ट explicit label syntax के बजाय implicit labels (function के नाम पर, जैसे forEach$0) का उपयोग करना।
चैप्टर सारांश
  • किसी inline function को पास किए lambda के अंदर एक साधारण return एक non-local return करता है, enclosing function से पूरी तरह बाहर निकलते हुए।
  • किसी lambda को name@ { ... } से label करना और return@name उपयोग करना सिर्फ उस lambda से return करता है, पूरे function से नहीं।
  • Kotlin उस function के नाम से मेल खाता एक implicit label भी प्रदान करता है जिसे lambda पास किया गया है, जैसे return@forEach।
  • सही तरह का return (local बनाम non-local) चुनना higher-order functions से बने loops में अचानक control-flow bugs से बचाता है।
🔒

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.