← Back to Kotlin Course | Chapter 4: Functions | Lesson 4 of 7

Single-Expression Functions क्या हैं

एक single-expression function curly braces को पूरी तरह छोड़ देता है जब पूरा काम एक छोटे formula के रूप में लिखा जा सके।
Syntax
kotlin
fun functionName(param: Type) = expression

fun functionName(param: Type): ReturnType = if (condition) a else b

Writing a Single-Expression Function

{ return ... } के बजाय, किसी function का body एक single = expression हो सकता है, जो simple computations के लिए छोटा और अक्सर साफ़ दोनों है।

उदाहरण: Writing a Single-Expression Function

markup
fun square(x: Int) = x * x

// Entry point: execution of the program starts here
fun main() {
    // Print "Square of 6: ${square(6)}" to the console, with a trailing newline
    println("Square of 6: ${square(6)}")
}

Return Type Inference

Kotlin किसी single-expression function का return type खुद expression से infer कर लेता है, इसलिए इसे explicitly लिखना आमतौर पर ज़रूरी नहीं।

Note: बड़े projects में public API functions के लिए documentation की स्पष्टता के लिए अब भी एक explicit return type चाहिए।

उदाहरण: Return Type Inference

markup
fun greeting(name: String) = "Hello, $name!"

// Entry point: execution of the program starts here
fun main() {
    // Print `greeting("Kotlin")` to the console, with a trailing newline
    println(greeting("Kotlin"))
}

Using if/when Expressions

चूंकि Kotlin में if और when expressions हैं, एक single-expression function body सिर्फ एक expression रहते हुए काफी समृद्ध logic रख सकता है।

उदाहरण: Using if/when Expressions

markup
fun classify(n: Int) = if (n % 2 == 0) "even" else "odd"

// Entry point: execution of the program starts here
fun main() {
    // Print "7 is ${classify(7)}" to the console, with a trailing newline
    println("7 is ${classify(7)}")
    // Print "8 is ${classify(8)}" to the console, with a trailing newline
    println("8 is ${classify(8)}")
}

When Not to Use This Style

Single-expression syntax को छोटे, genuinely single-expression logic के लिए ही रखना सबसे अच्छा है; कई statements या intermediate variables चाहने वाली किसी भी चीज़ को इसके बजाय एक normal block body उपयोग करनी चाहिए।

उदाहरण: When Not to Use This Style

markup
// Define the function `describeNumber` taking `n` and returning a String
fun describeNumber(n: Int): String {
    // Declare a read-only variable `parity`, set to `if (n % 2 == 0) "even" else "odd"`
    val parity = if (n % 2 == 0) "even" else "odd"
    // Declare a read-only variable `sign`, set to `if (n >= 0) "non-negative" else "negative"`
    val sign = if (n >= 0) "non-negative" else "negative"
    // Return `"$n is $parity and $sign"` from this function
    return "$n is $parity and $sign"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `describeNumber(-4)` to the console, with a trailing newline
    println(describeNumber(-4))
}
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. = sign भूल जाना; single-expression functions block body के बजाय = expression उपयोग करते हैं।
  2. किसी single-expression function में कई statements लिखने की कोशिश करना; इसे बिल्कुल एक expression होना चाहिए।
  3. जब इसे expression से सुरक्षित रूप से infer किया जा सके तब भी एक अनावश्यक explicit return type जोड़ना।
चैप्टर सारांश
  • एक single-expression function को fun name(params) = expression के रूप में, बिना curly braces के, लिखा जाता है।
  • return type आमतौर पर expression से infer किया जा सकता है, इसलिए इसे अक्सर छोड़ दिया जाता है।
  • यह style छोटे, simple computations के लिए उपयुक्त है, multi-statement logic के लिए नहीं।
  • एक single-expression function अब भी block-bodied की तरह ही parameters और default values support करता है।
🔒

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.