Named Arguments क्या हैं
In this page:
fun functionName(param1: Type, param2: Type = defaultValue) { }
functionName(param1 = value1, param2 = value2)
functionName(param2 = value2, param1 = value1)
Basic Named Arguments
position पर निर्भर रहने के बजाय, आप किसी function को call करते समय explicitly parameterName = value specify कर सकते हैं, जो call को self-documenting बनाता है।
उदाहरण: Basic Named Arguments
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
// Return `"Rectangle ${width}x$height"` from this function
return "Rectangle ${width}x$height"
}
// Entry point: execution of the program starts here
fun main() {
// Print `createRectangle(width = 10, height = 5)` to the console, with a trailing newline
println(createRectangle(width = 10, height = 5))
}
Login to try C/C++/Java/PHP code in the editor
Reordering with Named Arguments
चूंकि named arguments position के बजाय नाम से match होते हैं, उन्हें declare होने से अलग order में पास किया जा सकता है।
उदाहरण: Reordering with Named Arguments
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
// Return `"Rectangle ${width}x$height"` from this function
return "Rectangle ${width}x$height"
}
// Entry point: execution of the program starts here
fun main() {
// Print `createRectangle(height = 5, width = 10)` to the console, with a trailing newline
println(createRectangle(height = 5, width = 10))
}
Login to try C/C++/Java/PHP code in the editor
Skipping Defaults with Named Arguments
Named arguments आपको सिर्फ एक बाद वाले default parameter को override करने देते हैं बिना उन पहले वाले defaults को दोहराए जिन्हें आप रखना चाहते हैं।
उदाहरण: Skipping Defaults with Named Arguments
// Define the function `createUser` taking `name`, `age`, `active` and returning a String
fun createUser(name: String, age: Int = 18, active: Boolean = true): String {
// Return `"$name (age $age, active=$active)"` from this function
return "$name (age $age, active=$active)"
}
// Entry point: execution of the program starts here
fun main() {
// Print `createUser("Carol", active = false)` to the console, with a trailing newline
println(createUser("Carol", active = false))
}
Login to try C/C++/Java/PHP code in the editor
Improving Readability
एक जैसे type के कई parameters वाले functions के लिए, जैसे कई booleans, named arguments call site को एक नज़र में समझना कहीं आसान बना देते हैं।
उदाहरण: Improving Readability
// Define the function `configure` taking `darkMode`, `notifications`, `autoSave`
fun configure(darkMode: Boolean, notifications: Boolean, autoSave: Boolean) {
// Print "dark=$darkMode, notify=$notifications, autoSave=$autoSave" to the console, with a trailing newline
println("dark=$darkMode, notify=$notifications, autoSave=$autoSave")
}
// Entry point: execution of the program starts here
fun main() {
configure(darkMode = true, notifications = false, autoSave = true)
}
Login to try C/C++/Java/PHP code in the editor
- named और positional arguments को गलत तरीके से मिलाना; एक बार आप एक named argument उपयोग करें, उसके बाद के arguments को आमतौर पर named भी होना चाहिए।
- यह मान लेना कि named arguments performance या behavior बदलते हैं; वे सिर्फ readability और call-site flexibility को प्रभावित करते हैं।
- कई
Booleanparameters वाले functions के लिए named arguments का उपयोग न करना, call sites को पढ़ना मुश्किल बनाते हुए (जैसेf(true, false, true))।
- Named arguments
parameterName = valueके रूप में पास किए जाते हैं, declaration order से स्वतंत्र। - वे कई parameters या एक जैसे type के कई parameters वाले functions के लिए readability को काफी सुधारते हैं।
- Named arguments को default parameters के साथ combine करके बाद वाला सेट करते हुए पहले वाले defaults छोड़े जा सकते हैं।
- एक बार named argument उपयोग होने पर, उस call में बाद के किसी भी arguments को स्पष्टता के लिए भी named होना चाहिए (और कभी-कभी होना ज़रूरी है)।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: