Omitting argument labels with _ in Swift - Time & Space Complexity
We want to understand how the time it takes to run a Swift function changes when we omit argument labels using _.
Does skipping argument labels affect how long the function takes to run?
Analyze the time complexity of the following Swift function that sums numbers.
func sumNumbers(_ numbers: [Int]) -> Int {
var total = 0
for number in numbers {
total += number
}
return total
}
let result = sumNumbers([1, 2, 3, 4, 5])
This function adds up all numbers in an array passed without an argument label.
Look for loops or repeated steps in the code.
- Primary operation: The
forloop that adds each number tototal. - How many times: Once for each number in the input array.
The function adds each number one by one, so more numbers mean more additions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The number of operations grows directly with the number of input items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Omitting the argument label with _ makes the function faster."
[OK] Correct: The argument label only changes how you call the function, not how many steps it takes inside.
Understanding how function call syntax affects performance helps you explain code clearly and confidently in interviews.
What if the function used two arrays as inputs, both without argument labels? How would the time complexity change?
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
