Variadic parameters in Swift - Time & Space Complexity
When using variadic parameters, we want to know how the time to run the function changes as we pass more values.
How does the function handle many inputs and how long does it take?
Analyze the time complexity of the following code snippet.
func sum(numbers: Int...) -> Int {
var total = 0
for number in numbers {
total += number
}
return total
}
This function adds up all the numbers passed to it using a variadic parameter.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the variadic parameter.
- How many times: Once for each number passed to the function.
As you add more numbers, the function does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of inputs.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of inputs.
[X] Wrong: "Variadic parameters make the function run instantly no matter how many inputs."
[OK] Correct: The function still processes each input one by one, so more inputs mean more work.
Understanding how variadic parameters affect time helps you explain how your code scales when handling many inputs.
"What if we changed the function to multiply the numbers instead of adding? How would the time complexity change?"
Practice
Solution
Step 1: Understand variadic parameter purpose
Variadic parameters let a function accept zero or more values of the same type.Step 2: Compare options with definition
Only Accept any number of values of the same type correctly describes this behavior.Final Answer:
Accept any number of values of the same type -> Option BQuick Check:
Variadic parameter = multiple values [OK]
- Confusing variadic with multiple return values
- Thinking it accepts different types
- Assuming it limits arguments
Solution
Step 1: Recall variadic syntax in Swift
Variadic parameters use three dots (...) after the type.Step 2: Check each option
func sum(numbers: Int...) { } uses 'Int...' which is correct syntax for variadic parameters.Final Answer:
func sum(numbers: Int...) { } -> Option CQuick Check:
Variadic syntax = Type... [OK]
- Using asterisk (*) instead of three dots
- Using array type instead of variadic
- Missing dots after type
func greet(names: String...) {
for name in names {
print("Hello, \(name)!")
}
}
greet(names: "Anna", "Bob")Solution
Step 1: Understand variadic parameter behavior
The parameter 'names' collects all arguments into an array of strings.Step 2: Trace the for loop output
The loop prints "Hello, Anna!" and then "Hello, Bob!" each on a new line.Final Answer:
Hello, Anna!\nHello, Bob! -> Option AQuick Check:
Variadic parameter prints each argument separately [OK]
- Thinking it prints all names in one line
- Assuming a syntax error
- Confusing parameter name with output
func multiply(numbers: Int...) -> Int {
var result = 1
for i in 0..Solution
Step 1: Analyze function call
The call 'multiply(numbers: )' passes no arguments, which is allowed but syntax requires no empty parentheses.Step 2: Understand Swift call syntax for variadic
Calling with empty parentheses after variadic parameter name is invalid; omit arguments or provide values.Final Answer:
Calling multiply without any arguments causes an error -> Option DQuick Check:
Empty variadic call syntax invalid [OK]
- Thinking for loop is wrong
- Believing Int can't be variadic
- Missing return statement error
Solution
Step 1: Use variadic parameter syntax
func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } correctly uses 'Int...' to accept any number of integers.Step 2: Calculate average as Double
It sums all numbers and divides by count, converting to Double for decimal average.Final Answer:
func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } -> Option AQuick Check:
Variadic + Double average calculation = func average(numbers: Int...) -> Double { Double(numbers.reduce(0, +)) / Double(numbers.count) } [OK]
- Using array instead of variadic parameter
- Returning Int instead of Double for average
- Using invalid syntax like Int*
Ready to prove it?
Pass the test and this pattern moves unknown
