Recall & Review
beginner
What is a variadic parameter in Swift?
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: <br>
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?<br>
func greet(names: String...) {<br> for name in names {<br> print("Hello, \(name)!")<br> }<br>}This function takes any number of names as input and prints a greeting for each one. For example, calling
greet(names: "Anna", "Bob") prints:<br>Hello, Anna!<br>Hello, Bob!Click to reveal answer
How do you declare a variadic parameter in Swift?
✗ Incorrect
Variadic parameters use three dots (...) after the type to accept multiple values.
How many variadic parameters can a Swift function have?
✗ 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:
✗ 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?
✗ 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?
✗ 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.