Complete the code to define a function that accepts a variable number of integers.
func sum(numbers: [1]) -> Int { return numbers.reduce(0, +) }
The ...Int syntax defines a variadic parameter that accepts zero or more integers.
Complete the code to call the variadic function with three integer arguments.
let result = sum(numbers: [1])
Variadic parameters are passed as comma-separated values, not as arrays or tuples.
Fix the error in the function definition to correctly accept a variable number of strings.
func greet(names: [1]) { for name in names { print("Hello, \(name)!") } }
The correct syntax for variadic parameters requires three dots before the type, like ...String.
Fill both blanks to create a function that returns the average of a variable number of Double values.
func average(values: [1]) -> Double { let total = values[2]reduce(0.0, +) return total / Double(values.count) }
The variadic parameter uses ...Double. To call reduce on the array, use the dot operator ..
Fill all three blanks to define and call a function that prints all provided strings separated by commas.
func printAll(items: [1]) { let combined = items[2]joined(separator: [3]) print(combined) } printAll(items: "apple", "banana", "cherry")
The function uses ...String for variadic parameters. The joined(separator:) method is called with a dot and a string separator ", ".