Chapter    8

Functions

Functions are self-contained blocks of code that perform a specific task. Swift’s functions are very flexible. They can be very simple, such as the print function, or very complex. Here are some examples:

  • They can be simple C-style functions with no parameter names.
  • They can have local and external parameter names for parameters, similar to Objective-C.
  • They can have parameters that provide default values.
  • They can return multiple values.
  • They can be passed as arguments.
  • They can be returned as a result from other functions.
  • They can have nested functions.

Defining Functions

The general format for a function starts with the keyword func, followed by the name of the function, optional arguments, and an optional return ...

Get Learn Swift 2 on the Mac, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.