Chapter    8

Functions and Closures

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 println or print functions, or very complex.

  • They can be simple C-style functions with no parameter names.
  • They can have local and external parameter names for paramters, 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 on the Mac: For OS X and iOS 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.