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.

An unlabeled parameter in Swift is a function, method, or initializer parameter that explicitly omits the external argument label at the call site. This is achieved by substituting the standard argument label with an underscore (_) in the function signature, requiring the caller to pass the argument strictly by its positional order rather than by a named key. In Swift, parameters possess both an argument label (used externally when invoking the function) and a parameter name (used internally within the function’s lexical scope). By default, the parameter name also serves as the argument label. To suppress the external label, the _ wildcard is placed before the internal parameter name.

Syntax

func functionName(_ parameterName: DataType) {
    // 'parameterName' is used internally
}

Technical Behavior

When a parameter is unlabeled, the compiler enforces positional matching at the call site. The internal parameter name remains fully accessible within the function body, but attempting to use the internal name as a label during invocation will result in a compiler error.
// Declaration
func processInput(_ input: String) {
    // 'input' is the internal parameter name
    let processed = input.uppercased()
    print(processed)
}

// Call site: The argument is passed positionally without a label
processInput("Data string") 

// ERROR: Extraneous argument label 'input:' in call
// processInput(input: "Data string") 

Mixed Parameter Labeling

Functions can freely mix labeled and unlabeled parameters. The underscore only affects the specific parameter it precedes.
// Declaration mixing unlabeled and labeled parameters
func configureSystem(_ environment: String, timeout: Int, _ retryCount: Int) {
    // 'environment' and 'retryCount' are unlabeled externally
    // 'timeout' retains its default external label
}

// Call site
configureSystem("Production", timeout: 30, 5)

Initializer Application

The unlabeled parameter syntax applies identically to custom initializers (init), overriding Swift’s default behavior of generating argument labels for all initialization parameters.
struct Vector {
    let x: Double
    let y: Double

    // Unlabeled parameters in an initializer signature
    init(_ x: Double, _ y: Double) {
        self.x = x
        self.y = y
    }
}

// Instantiation without argument labels
let vector = Vector(10.0, 20.0)
Master Swift with Deep Grasping Methodology!Learn More