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?