Skip to content

Latest commit

 

History

History
88 lines (66 loc) · 3.99 KB

6.1 - Argument Labels and Parameter Names.md

File metadata and controls

88 lines (66 loc) · 3.99 KB

By default in Swift, function parameters have two names:

  • An argument label - The name used when supplying the argument to the function. The intention behind argument labels is for functions to be able to called in expressive sentence-like fashion.
  • A parameter name - The name of the parameter used in the internal implementation

By default, parameters use their parameter name as their argument label.

Specifying Argument Labels

To specify an argument label for a parameter, write it before the parameter name, like so:

func someFunction(argumentLabel parameterName: Int) {
    // In the function body, parameterName refers to the argument value
    // for that parameter.
}

Omitting Argument Labels

If you don't want a parameter to have an argument label, replace it with an _:

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(1, secondParameterName: 2)

Note than if a parameter has an argument label, it must be labeled when you call the function.

Default Parameter Values

Default parameter values are specified in an analagous way to other languages. As in other languages, make sure to put parameters without default values before parameters with them. An example:

func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {
    // If you omit the second argument when calling this function, then
    // the value of parameterWithDefault is 12 inside the function body.
}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12

Variadic Parameters

A variadic parameter is one that accepts 0 or more values of a specified type. They are declared in a function by adding ... after they parameter's type.

Variadic parameters are made available within the function's type as an array of the appropriate type. Note that a function can have at most one variadic parameter.

The example below shows how you can use variadic parameters to calculate an aritmetic mean:

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
arithmeticMean(1, 2, 3, 4, 5)
// returns 3.0, which is the arithmetic mean of these five numbers
arithmeticMean(3, 8.25, 18.75)
// returns 10.0, which is the arithmetic mean of these three numbers

In-Out Parameters

By default, function parameters are constants; you cannot change the value of a function parameter from within the function's body and persist those changes after the function call has ended. However, you can opt-in to this behaviour by using the inout keyword.

In-out parameters are passed in to a function, changed by that function, and then passed back out of the function to replace the initial value. Only variables can be passed in as in-out parameters, and you must place an ampersand(&) before the variable's name to indicate that it can be modified by the function. This is all shown in the below function, which swaps two ints:

func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

Previous Note | Back To Contents | Next Note