Sometimes you want to call a function without writing the argument label every time. Using _ lets you skip the label for cleaner calls.
Omitting argument labels with _ in Swift
Introduction
Syntax
Swift
func functionName(_ parameterName: Type) { // function body }
The underscore _ before a parameter name means you don't have to write the label when calling the function.
This only affects how you call the function, not how you use the parameter inside the function.
Examples
greet without writing the label name: because of the _.Swift
func greet(_ name: String) { print("Hello, \(name)!") } greet("Alice")
Swift
func add(_ a: Int, _ b: Int) -> Int { return a + b } let sum = add(3, 5) print(sum)
factor1, the second omits it with _.Swift
func multiply(factor1 a: Int, _ b: Int) -> Int { return a * b } let result = multiply(factor1: 4, 5) print(result)
Sample Program
This program defines a function that greets a person. The argument label is omitted, so you just pass the name directly.
Swift
func sayHello(_ person: String) { print("Hi, \(person)!") } sayHello("Bob")
Important Notes
Using _ can make your code cleaner but be careful not to lose clarity.
Omitting labels is common in Swift's standard library functions like print().
Summary
Use _ to skip argument labels when calling functions.
This makes function calls shorter and sometimes easier to read.
Inside the function, you still use the parameter name as usual.
Practice
1. What does the underscore
_ do when used before a function parameter in Swift?easy
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]
Hint: Underscore means skip argument label when calling [OK]
Common Mistakes:
- Thinking underscore makes parameter optional
- Confusing underscore with type changes
- Assuming underscore affects parameter mutability
2. Which of the following function declarations correctly omits the argument label for the first parameter?
easy
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]
Hint: Underscore must be alone before parameter name [OK]
Common Mistakes:
- Putting underscore after parameter name
- Adding spaces between underscore and name incorrectly
- Using underscore as part of parameter name
3. What is the output of this Swift code?
func multiply(_ a: Int, _ b: Int) -> Int {
return a * b
}
print(multiply(3, 4))medium
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]
Hint: Underscore means call without labels, so multiply(3,4) works [OK]
Common Mistakes:
- Expecting argument labels in call
- Confusing output with string of function call
- Adding parameters incorrectly
4. Identify the error in this Swift function call:
func divide(_ numerator: Double, denominator: Double) -> Double {
return numerator / denominator
}
divide(10, denominator: 2)medium
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]
Hint: First param no label, second param label required [OK]
Common Mistakes:
- Expecting both labels omitted
- Trying to omit second label incorrectly
- Confusing parameter order
5. You want to write a Swift function
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?hard
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]
Hint: Use underscore for all params to omit all labels [OK]
Common Mistakes:
- Omitting only one label but calling without both
- Mixing labeled and unlabeled parameters incorrectly
- Forgetting underscore syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
