Single-Expression Functions क्या हैं
In this page:
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
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)}")
}
Login to try C/C++/Java/PHP code in the editor
Return Type Inference
Kotlin किसी single-expression function का return type खुद expression से infer कर लेता है, इसलिए इसे explicitly लिखना आमतौर पर ज़रूरी नहीं।
उदाहरण: Return Type Inference
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"))
}
Login to try C/C++/Java/PHP code in the editor
Using if/when Expressions
चूंकि Kotlin में if और when expressions हैं, एक single-expression function body सिर्फ एक expression रहते हुए काफी समृद्ध logic रख सकता है।
उदाहरण: Using if/when Expressions
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)}")
}
Login to try C/C++/Java/PHP code in the editor
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
// 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))
}
Login to try C/C++/Java/PHP code in the editor
=sign भूल जाना; single-expression functions block body के बजाय= expressionउपयोग करते हैं।- किसी single-expression function में कई statements लिखने की कोशिश करना; इसे बिल्कुल एक expression होना चाहिए।
- जब इसे 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: