Functions

are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with names and argument labels for each parameter. Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution.

Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.

Function Argument Labels and Parameter Names

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

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

All parameters must have unique names. Although it’s possible for multiple parameters to have the same argument label, unique argument labels help make your code more readable.

Specifying Argument Labels

You write an argument label before the parameter name, separated by a space:

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

Here’s a variation of the greet(person:) function that takes a person’s name and hometown and returns a greeting:

  1. func greet(person: String, from hometown: String) -> String {
  2. return "Hello \(person)! Glad you could visit from \(hometown)."
  3. }
  4. print(greet(person: "Bill", from: "Cupertino"))
  5. // Prints "Hello Bill! Glad you could visit from Cupertino."

The use of argument labels can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that’s readable and clear in intent.

Omitting Argument Labels

If you don’t want an argument label for a parameter, write an underscore (_) instead of an explicit argument label for that parameter.

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

If a parameter has an argument label, the argument must be labeled when you call the function.