Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A variadic parameter accepts zero or more values of a specified type. It allows a function to be invoked with a varying number of input arguments for a single parameter, abstracting away the need to explicitly construct and pass an array at the call site. To declare a variadic parameter, append three periods (...) immediately after the parameter’s type name.
func functionName(parameterName: Type...) {
    // Implementation
}
Within the function body, the Swift compiler automatically packs the comma-separated arguments passed to the variadic parameter into an array of the specified type. For example, a variadic parameter declared as Int... is exposed internally as [Int].
func process(values: Int...) {
    // 'values' is evaluated as [Int]
    for value in values {
        print(value)
    }
}

// Call site accepts a comma-separated list of values
process(values: 1, 2, 3, 4)

// Call site also accepts zero arguments
process()

Technical Constraints and Rules

When implementing variadic parameters, the Swift compiler enforces specific structural rules to guarantee unambiguous parsing at the call site: 1. Multiple Variadic Parameters As of Swift 5.4, a single function signature can declare multiple variadic parameters. The compiler resolves the boundaries between them using argument labels.
// Valid: Multiple variadic parameters separated by explicit labels
func evaluate(scores: Int..., names: String...) {
    let scoreArray: [Int] = scores
    let nameArray: [String] = names
}
2. Parameter Resolution and Ambiguity If a variadic parameter is followed by one or more standard parameters, the first parameter immediately following the variadic parameter must possess an explicit argument label. This prevents compiler ambiguity by clearly defining where the variadic sequence terminates.
// Valid: 'multiplier' label explicitly ends the 'values' variadic sequence
func scale(values: Double..., multiplier: Double) { }

scale(values: 1.5, 2.5, 3.5, multiplier: 2.0)
3. Mutability Restrictions Variadic parameters are strictly read-only within the function scope. They cannot be marked with the inout keyword. Attempting to declare an inout Type... parameter will result in a compile-time error. 4. Array Passing You cannot directly pass an existing array to a variadic parameter. The variadic syntax strictly expects a comma-separated list of elements. If you possess an array, you must either change the function signature to accept an array ([Type]) or manually extract the elements, as Swift does not currently support a spread operator (like ...array) at the call site.
Master Swift with Deep Grasping Methodology!Learn More