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

नाम वाले Arguments

Named arguments आपको किसी function को call करते समय हर input को label करने देते हैं, जैसे किसी order पर 'size: large, flavor: mint' लिखना। आप इन्हें किसी भी क्रम में दे सकते हैं।

In this page:

  1. Named Arguments
Syntax
scala
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

markup
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)
}
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. किसी parameter का नाम गलत लिखना, जैसे describe(nme = "Al"), जो fail हो जाता है क्योंकि नाम मेल खाना चाहिए।
  2. named arguments के बाद positional arguments को इस तरह मिलाना कि वे अब position से मेल नहीं खाते, जिससे कोई argument दो बार पास हो जाता है।
  3. एक ही parameter को positionally और नाम दोनों से देना, जिससे parameter specified twice error आता है।
🔒

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.