0
0
Swiftprogramming~5 mins

Omitting argument labels with _ in Swift - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Omitting argument labels with _
O(n)
Understanding Time 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?

Scenario Under Consideration

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.

Identify Repeating Operations

Look for loops or repeated steps in the code.

  • Primary operation: The for loop that adds each number to total.
  • How many times: Once for each number in the input array.
How Execution Grows With Input

The function adds each number one by one, so more numbers mean more additions.

Input Size (n)Approx. Operations
1010 additions
100100 additions
10001000 additions

Pattern observation: The number of operations grows directly with the number of input items.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows in a straight line as the input size grows.

Common Mistake

[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.

Interview Connect

Understanding how function call syntax affects performance helps you explain code clearly and confidently in interviews.

Self-Check

What if the function used two arrays as inputs, both without argument labels? How would the time complexity change?