O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    27

Functions

You can declare a function anywhere with the func keyword. Functions are used to organize code into reusable chunks that take input parameters and return results. Listing 27-1 shows an example of a function that returns the string “Hello World.”

Listing 27-1. Simple Function

func getPhrase() -> String{    return "Hello World"}

In Listing 27-1, the function declaration starts with the func keyword followed by the name of the function. After the function name in Listing 27-1, you have (), which designates an empty parameter list. The return type of the function (String) is written after the return type symbol ->.

After the function return type, you have a code block designated in the curly brackets, {}. The getPhrase() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required