← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 1 of 6

Lambda Syntax क्या है

एक lambda एक छोटा नामहीन function है जिसे आप तुरंत लिख सकते हैं और किसी दूसरे को सौंप सकते हैं, जैसे instructions वाली एक sticky note।
Syntax
kotlin
val lambda = { param: Type -> expression }
val noParams = { expression }
val multi = { a: Type, b: Type -> expression }

lambda(argument)

Writing a Basic Lambda

एक lambda curly braces के अंदर लिखा जाता है, parameters (अगर कोई हों) को एक arrow -> से पहले list करते हुए, उसके बाद body। body में आखिरी expression lambda का result बन जाता है।

उदाहरण: Writing a Basic Lambda

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `square`, set to `{ x: Int -> x * x }`
    val square = { x: Int -> x * x }
    // Print "Square of 5: ${square(5)}" to the console, with a trailing newline
    println("Square of 5: ${square(5)}")
}

Lambdas with No Parameters

जब कोई lambda कोई parameters नहीं लेता, arrow और parameter list को बस छोड़ दिया जाता है, braces के अंदर सिर्फ body छोड़ते हुए।

उदाहरण: Lambdas with No Parameters

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `sayHello`, set to `{ println("Hello from a lambda!") }`
    val sayHello = { println("Hello from a lambda!") }
    sayHello()
}

Lambdas with Multiple Parameters

एक lambda कई parameters ले सकता है, arrow से पहले commas से अलग करते हुए, बिल्कुल किसी regular function की parameter list की तरह।

उदाहरण: Lambdas with Multiple Parameters

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `add`, set to `{ a: Int, b: Int -> a + b }`
    val add = { a: Int, b: Int -> a + b }
    // Print "Sum: ${add(3, 4)}" to the console, with a trailing newline
    println("Sum: ${add(3, 4)}")
}

Passing a Lambda to a Function

Lambdas अक्सर पहले एक variable में store होने के बजाय उस function के call site पर सीधे लिखे जाते हैं जो एक की उम्मीद करता है, जैसे collection operations के साथ।

उदाहरण: Passing a Lambda to a Function

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
    val numbers = listOf(1, 2, 3, 4)
    // Declare a read-only variable `doubled`, set to `numbers.map { n -> n * 2 }`
    val doubled = numbers.map { n -> n * 2 }
    // Print `doubled` to the console, with a trailing newline
    println(doubled)
}
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. जब parameters explicitly नामित हों तब किसी lambda के parameters को उसकी body से अलग करने वाला arrow -> भूल जाना।
  2. किसी lambda की parameter list के चारों ओर अनावश्यक parentheses जोड़ना; lambda parameters किसी regular function की तरह () में wrap नहीं होते।
  3. किसी variable को assign किए lambda के अंदर return लिखने की कोशिश करना और उम्मीद करना कि यह सिर्फ lambda से बाहर निकलेगा, जबकि अगर lambda inlined है तो यह असल में एक non-local return करता है।
चैप्टर सारांश
  • एक lambda expression { parameters -> body } के रूप में लिखा जाता है, और इसे किसी variable को assign किया जा सकता है या सीधे किसी function को पास किया जा सकता है।
  • अगर किसी lambda में कोई parameters नहीं हैं, -> और उससे पहले की हर चीज़ पूरी तरह छोड़ दी जाती है।
  • किसी lambda की body में आखिरी expression अपने आप इसकी return value है।
  • Lambdas को एक function type के साथ variables में store किया जा सकता है, जैसे val add: (Int, Int) -> Int।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.