April 2026
Intermediate
683 pages
25h 29m
English
Let’s get warmed up by defining functions in their simplest form and see how to call them. We will also discuss the subject of variable scopes for functions.
In Swift syntax, a function is expressed with the func keyword, which is an obvious abbreviation. Listing 5.1 demonstrates a very simple function definition.
func sayHello() { print("Hello from the function!")}
Listing 5.1 Simple Function
The func sayHello() expression is telling Swift that we are about to declare a new function, which is called sayHello. Following that, we put the function code into the {…} brackets. Easy, right? This is not unlike the way loops work, which you learned about in Chapter 4.
Although ...
Read now
Unlock full access