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

Argument Labels क्या हैं

Argument labels वे friendly words हैं जो आप किसी function call करते समय एक value से पहले type करते हैं, ताकि call एक sentence जैसे पढ़े।
Syntax
swift
func functionName(label name: Type) { }     // custom label
func functionName(_ name: Type) { }         // no label

functionName(label: value)
functionName(value)

Default Argument Labels

Default रूप से, एक function call करने के लिए हर parameter के नाम को एक label के रूप में उपयोग करना ज़रूरी है, जो call को self-documenting बनाता है।

उदाहरण: Default Argument Labels

markup
// Define the function `multiply` taking `x`, `y` and returning an Int
func multiply(x: Int, y: Int) -> Int {
    // Return `x * y` from this function
    return x * y
}
// Print `multiply(x: 6, y: 7)` to the console
print(multiply(x: 6, y: 7))

Custom External Labels

आप एक parameter को callers के लिए function body के अंदर उपयोग होने वाले नाम से अलग एक label दे सकते हैं, call site पर readability improve करते हुए।

Note: यहां person वह है जो callers type करते हैं, जबकि name function body के अंदर उपयोग होता है।

उदाहरण: Custom External Labels

markup
// Define the function `greet` taking `name` and returning a String
func greet(person name: String) -> String {
    // Return `"Hello, \(name)"` from this function
    return "Hello, \(name)"
}
// Print `greet(person: "Alice")` to the console
print(greet(person: "Alice"))

Omitting Labels with Underscore

External label के रूप में एक underscore _ उपयोग करना function को उस particular argument का नाम दिए बिना call होने देता है।

उदाहरण: Omitting Labels with Underscore

markup
// Define the function `square` taking `number` and returning an Int
func square(_ number: Int) -> Int {
    // Return `number * number` from this function
    return number * number
}
// Print `square(5)` to the console
print(square(5))
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. Argument label (call site पर उपयोग होता है) को parameter name (function body के अंदर उपयोग होता है) के साथ confuse करना -- ये अलग words हो सकते हैं।
  2. यह भूल जाना कि label के रूप में _ उपयोग करना call करते समय बिल्कुल एक label लिखने की requirement हटा देता है।
  3. यह मान लेना कि सभी Swift functions को labels चाहिए; convention से पहले parameter का label अक्सर छोड़ा जाता है जब यह बिना किसी के naturally पढ़े।
चैप्टर सारांश
  • Default रूप से, किसी parameter का नाम इसके external label और internal name दोनों के रूप में काम करता है।
  • आप internal name से पहले एक अलग external label specify कर सकते हैं: func f(externalLabel internalName: Type)।
  • External label के रूप में _ उपयोग करना function को उस argument का नाम दिए बिना call होने देता है।
  • अच्छे argument labels call sites को natural English जैसे पढ़ाते हैं।
🔒

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.