Functions and Methods

Learn how to implement Swift methods and functions and explore parameters, arguments, and return values.

Swift functions, methods, and closures are a vital part of writing well-structured and efficient code. It also provides a way to organize programs while avoiding code repetition. In this section of the course, we will look at how functions, methods, and closures are declared and used within Swift.

What is a function?

A function is a named block of code that can be called upon to perform a specific task. It can be provided data on which to perform the task and is capable of returning results to the code that called it. For example, if a particular arithmetic calculation needs to be performed in a Swift program, the code to perform the arithmetic can be placed in a function. The function can be programmed to accept the values on which the arithmetic is to be performed (referred to as parameters) and returns the result of the calculation. At any point in the program code where the calculation is required, the function is simply called, the parameter values are passed through as arguments, and the result is returned.

Parameter or argument?

The terms parameter and argument are often used interchangeably when discussing functions. There is, however, a subtle difference. The values that a function is able to accept when it is called are referred to as parameters. At the point that the function is actually called and passed those values, however, are referred to as arguments.

What is a method?

A method is essentially a function that is associated with a particular class, structure, or enumeration. For example, if you declare a function within a Swift class (a topic covered in detail in the upcoming “Swift Object-Oriented Programming” lesson), it is considered to be a method. Although the remainder of this chapter refers to functions, the same rules and behavior apply equally to methods unless otherwise stated.

How to declare a Swift function

A Swift function is declared using the following syntax:

func <function name> (<para name>: <para type>, 
                      <para name>: <para type>, ... ) -> <return type> {
    // Function code
}

This combination of the function name, parameters, and return type is referred to as the function signature. Explanations of the various fields of the function declaration are as follows:

  • func : The prefix keyword used to notify the Swift compiler that this is a function or method.
  • <function name> : The name assigned to the function. This is the name by which the function will be referenced when it is called from within the application code.
  • <para name> : The name by which the parameter is to be referenced in the function code.
  • <para type> : The type of the corresponding parameter.
  • <return type> : The data type of the result returned by the function. If the function does not return a result, then no return type is specified.
  • Function code : The code of the function that does the work.

As an example, the following function takes no parameters, returns no result, and simply displays a message:

func sayHello() {
    print("Hello")
}

The following sample function, on the other hand, takes an Int and a String as parameters and returns a String result:

 ...