What if you could make your function calls as simple as talking to a friend?
Why Omitting argument labels with _ in Swift? - Purpose & Use Cases
Imagine you have a function that takes many inputs, and every time you call it, you have to write the name of each input. It feels like filling out a long form repeatedly, even when the names are obvious.
Writing argument labels every time is slow and makes your code bulky. It's like repeating yourself over and over, which can cause mistakes and make your code harder to read.
By using _ to omit argument labels, you can call functions more simply and cleanly. It's like skipping unnecessary words in a conversation to get straight to the point.
func greet(person name: String) { print("Hello, \(name)!") }
greet(person: "Anna")func greet(_ name: String) { print("Hello, \(name)!") }
greet("Anna")This lets you write shorter, clearer function calls that feel natural and easy to read.
Think of a calculator app where you just enter numbers and operations quickly without typing extra labels every time.
Writing argument labels every time can be repetitive and clutter code.
Using _ lets you skip labels for simpler calls.
This makes your code cleaner and easier to read.
Practice
_ do when used before a function parameter in Swift?Solution
Step 1: Understand the role of underscore in parameters
The underscore before a parameter name tells Swift to omit the argument label when calling the function.Step 2: Effect on function call
This means you can pass the argument directly without naming it, making the call shorter and simpler.Final Answer:
It allows you to call the function without writing the argument label. -> Option DQuick Check:
Underscore = omit argument label [OK]
- Thinking underscore makes parameter optional
- Confusing underscore with type changes
- Assuming underscore affects parameter mutability
Solution
Step 1: Identify correct underscore usage
The underscore must appear before the parameter name with a space, like_ name: String.Step 2: Check each option
func greet(_ name: String) { print("Hello, \(name)!") } uses_ name: Stringcorrectly. func greet(name: String) { print("Hello, \(name)!") } uses a label. func greet(name _ : String) { print("Hello, \(name)!") } has wrong syntax. func greet(_name: String) { print("Hello, \(_name)!") } uses an invalid parameter name.Final Answer:
func greet(_ name: String) { print("Hello, \(name)!") } -> Option BQuick Check:
Correct underscore syntax = func greet(_ name: String) { print("Hello, \(name)!") } [OK]
- Putting underscore after parameter name
- Adding spaces between underscore and name incorrectly
- Using underscore as part of parameter name
func multiply(_ a: Int, _ b: Int) -> Int {
return a * b
}
print(multiply(3, 4))Solution
Step 1: Understand function parameters with underscore
Both parameters have underscores, so argument labels are omitted when calling.Step 2: Calculate the function result
Calling multiply(3, 4) returns 3 * 4 = 12, which is printed.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Expecting argument labels in call
- Confusing output with string of function call
- Adding parameters incorrectly
func divide(_ numerator: Double, denominator: Double) -> Double {
return numerator / denominator
}
divide(10, denominator: 2)Solution
Step 1: Analyze parameter labels
The first parameter uses underscore, so no label needed. The second requires labeldenominator.Step 2: Check function call
The call usesdivide(10, denominator: 2), which matches the parameter labels correctly.Final Answer:
No error, code runs fine -> Option CQuick Check:
Underscore omits first label, second label required [OK]
- Expecting both labels omitted
- Trying to omit second label incorrectly
- Confusing parameter order
formatDate that takes a Date and a String format. You want to call it like formatDate(myDate, "yyyy-MM-dd") without argument labels. Which function declaration is correct?Solution
Step 1: Understand call without labels
CallingformatDate(myDate, "yyyy-MM-dd")means both parameters must omit labels.Step 2: Match function declaration
Only func formatDate(_ date: Date, _ format: String) -> String { /* implementation */ } uses underscore for both parameters, allowing call without labels.Final Answer:
func formatDate(_ date: Date, _ format: String) -> String { /* implementation */ } -> Option AQuick Check:
Both params underscore = no labels in call [OK]
- Omitting only one label but calling without both
- Mixing labeled and unlabeled parameters incorrectly
- Forgetting underscore syntax
Ready to prove it?
Pass the test and this pattern moves unknown
