A variadic parameter allows a function to accept zero or more values of a specified type as input. It is written with three dots (...) after the parameter's type.
Click to reveal answer
beginner
How do you declare a variadic parameter in a Swift function?
You add three dots (...) after the parameter type. For example: func sum(numbers: Int...) means the function can take many Int values.
Click to reveal answer
intermediate
Can a Swift function have more than one variadic parameter?
No, a function can have only one variadic parameter, and it must be the last parameter in the function's parameter list.
Click to reveal answer
beginner
How do you access the values passed to a variadic parameter inside the function?
Inside the function, the variadic parameter is treated as an array of the specified type. You can loop over it or access elements by index.
Click to reveal answer
beginner
Example: What does this Swift function do?
func greet(names: String...) { for name in names { print("Hello, \(name)!") } }
This function takes any number of names as input and prints a greeting for each one. For example, calling greet(names: "Anna", "Bob") prints: Hello, Anna! Hello, Bob!
Click to reveal answer
How do you declare a variadic parameter in Swift?
AAdd three dots (...) after the parameter type
BUse square brackets [] around the parameter
CUse an asterisk * before the parameter name
DDeclare the parameter as optional with ?
✗ Incorrect
Variadic parameters use three dots (...) after the type to accept multiple values.
How many variadic parameters can a Swift function have?
ATwo, but only if they are of the same type
BAny number, anywhere in the parameter list
COnly one, and it must be last
DNone, Swift does not support variadic parameters
✗ Incorrect
Swift allows only one variadic parameter per function, and it must be the last parameter.
Inside a function, a variadic parameter is treated as:
AAn array of the parameter's type
BA single value of the parameter's type
CA dictionary with keys as indexes
DA tuple of fixed size
✗ Incorrect
Variadic parameters are accessible as arrays inside the function.
What happens if you call a function with a variadic parameter but pass no arguments for it?
AThe function throws an error
BThe variadic parameter is an empty array
CThe function ignores the parameter
DThe function uses a default value
✗ Incorrect
If no arguments are passed, the variadic parameter is an empty array.
Which of these is a valid function declaration with a variadic parameter?
Afunc add(numbers: [Int]...)
Bfunc add(...numbers: Int)
Cfunc add(numbers: Int, others: Int...)
Dfunc add(numbers: Int...)
✗ Incorrect
The correct syntax places the three dots after the type and only one variadic parameter is allowed.
Explain what a variadic parameter is and how you use it in a Swift function.
Think about how you can pass many values to one parameter.
You got /3 concepts.
Describe the rules and limitations of using variadic parameters in Swift functions.
Consider function parameter order and count.
You got /3 concepts.
Practice
(1/5)
1. What does a variadic parameter in Swift allow a function to do?
easy
A. Limit the number of arguments to exactly two
B. Accept any number of values of the same type
C. Accept only one value of any type
D. Return multiple values from a function
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 B
Quick Check:
Variadic parameter = multiple values [OK]
Hint: Variadic means 'variable number' of same type values [OK]
Common Mistakes:
Confusing variadic with multiple return values
Thinking it accepts different types
Assuming it limits arguments
2. Which of the following is the correct way to declare a variadic parameter in Swift?
easy
A. func sum(numbers: Int*) { }
B. func sum(numbers: [Int]) { }
C. func sum(numbers: Int...) { }
D. func sum(numbers: Int) { }
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 C
Quick Check:
Variadic syntax = Type... [OK]
Hint: Look for three dots (...) after type for variadic [OK]
Common Mistakes:
Using asterisk (*) instead of three dots
Using array type instead of variadic
Missing dots after type
3. What is the output of this Swift code?
func greet(names: String...) {
for name in names {
print("Hello, \(name)!")
}
}
greet(names: "Anna", "Bob")
medium
A. Hello, Anna!\nHello, Bob!
B. Hello, names!
C. Hello, Anna, Bob!
D. Error: Cannot use variadic parameter here
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 A
Quick Check:
Variadic parameter prints each argument separately [OK]
Hint: Variadic acts like array; loop prints each item [OK]
Common Mistakes:
Thinking it prints all names in one line
Assuming a syntax error
Confusing parameter name with output
4. Identify the error in this Swift function using variadic parameters:
func multiply(numbers: Int...) -> Int {
var result = 1
for i in 0..
medium
A. The for loop syntax is incorrect
B. Missing return statement in function
C. Variadic parameters cannot be used with Int type
D. Calling multiply without any arguments causes an error
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 D
Quick Check:
Empty variadic call syntax invalid [OK]
Hint: Call variadic with values or omit parentheses if none [OK]
Common Mistakes:
Thinking for loop is wrong
Believing Int can't be variadic
Missing return statement error
5. How can you write a Swift function that accepts any number of integers and returns their average as a Double using variadic parameters?
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.