Argument Labels क्या हैं
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
// 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))
Login to try C/C++/Java/PHP code in the editor
Custom External Labels
आप एक parameter को callers के लिए function body के अंदर उपयोग होने वाले नाम से अलग एक label दे सकते हैं, call site पर readability improve करते हुए।
person वह है जो callers type करते हैं, जबकि name function body के अंदर उपयोग होता है।उदाहरण: Custom External Labels
// 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"))
Login to try C/C++/Java/PHP code in the editor
Omitting Labels with Underscore
External label के रूप में एक underscore _ उपयोग करना function को उस particular argument का नाम दिए बिना call होने देता है।
उदाहरण: Omitting Labels with Underscore
// 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))
Login to try C/C++/Java/PHP code in the editor
- Argument label (call site पर उपयोग होता है) को parameter name (function body के अंदर उपयोग होता है) के साथ confuse करना -- ये अलग words हो सकते हैं।
- यह भूल जाना कि label के रूप में
_उपयोग करना call करते समय बिल्कुल एक label लिखने की requirement हटा देता है। - यह मान लेना कि सभी 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: