Swift – Custom Label for Function Parameter(s)

To define a custom label for function’s parameter(s) in Swift, specify the custom label before the parameter name in the function definition.

By default the parameter name is used as label. But we can specify a custom label for parameter.

The syntax to specify a custom label for parameter in function definition is

func functionName(customLabel parameterName: Type) {
    //body
}

Example

In the following example, we will specify custom labels for parameters to add() function.

main.swift

func add(firstNumber a: Int, secondNumber b: Int) {
    print(a+b)
}

add(firstNumber: 10, secondNumber: 20)

Output

Swift - Custom Label for Function Parameter
ADVERTISEMENT

Conclusion

In this Swift Tutorial, we learned how to specify custom labels for parameters in function definition.