Swift - Function Parameter Label


By default, all parameters after the first one must have a label associated with them.

The label is necessary in calling the function.

This makes it easier to read the code.

When parameters have labels, it's a lot easier to remember what each parameter is for.

You can tell Swift to not require a label before the parameters by placing an underscore before their names:


func subtractNumbers(_ num1  : Int, _ num2  : Int) -> Int { 
    return num1 - num2 
var a = subtractNumbers(5, 3) // 2 
print(a)//www.j a  v a 2s.co m


By default, the label for the parameter is the same as the parameter's name.

You can provide a custom label for a parameter.

To override the default label for a parameter, you put the label before the parameter's name, like so:


func add(firstNumber num1  : Int, toSecondNumber num2: Int) -> Int { 
    return num1 + num2 
var a = add(firstNumber: 2, toSecondNumber: 3) // 5 
print(a)/*from w ww. j a  v  a  2s  . co m*/


Related Topic