नाम वाले Arguments
Named arguments आपको किसी function को call करते समय हर input को label करने देते हैं, जैसे किसी order पर 'size: large, flavor: mint' लिखना। आप इन्हें किसी भी क्रम में दे सकते हैं।
In this page:
Syntax
function_name(param2 = value2, param1 = value1)
Named Arguments
किसी function को paramName = value से call करने पर आप arguments को किसी भी क्रम में पास कर सकते हैं और call site self-documenting बन जाती है, खासकर एक जैसी type के कई parameters वाले functions के लिए उपयोगी।
Named और positional arguments को मिलाया जा सकता है, जब तक positional वाले पहले आएं। यह default parameters के साथ मिलकर खासकर सुविधाजनक है, जिससे आप शुरुआती defaults को छोड़ सकते हैं।
Note:
Named arguments किसी call को, जैसे
connect(host = "localhost", port = 8080), दो unlabeled positional values से कहीं ज़्यादा स्पष्ट बनाते हैं।उदाहरण: Named Arguments
object Main extends App {
def describe(name: String, age: Int, city: String): Unit = {
println(s"$name is $age years old and lives in $city")
}
describe(name = "Priya", city = "Mumbai", age = 28)
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- किसी parameter का नाम गलत लिखना, जैसे
describe(nme = "Al"), जो fail हो जाता है क्योंकि नाम मेल खाना चाहिए। - named arguments के बाद positional arguments को इस तरह मिलाना कि वे अब position से मेल नहीं खाते, जिससे कोई argument दो बार पास हो जाता है।
- एक ही parameter को positionally और नाम दोनों से देना, जिससे
parameter specified twiceerror आता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: