April 2026
Intermediate
683 pages
25h 29m
English
You know by now that functions can import parameters—both obligatory and optional ones. In this section, you will learn about a new feature: Functions can return values as well, which is often the result of the operation within the function.
To understand the syntax to return values, let’s start with a very simple example. Listing 5.25 contains a function that calculates the sum of two numbers and returns the result. Heads up: Returning the result is the new part!
func sumNumbers(_ n1: Int, _ n2: Int) -> Int { let sum = n1 + n2 return sum}
Listing 5.25 Simple Function Returning Sum of Two Numbers
This code snippet has two parts worth noting. First, the signature ends with the -> Int ...
Read now
Unlock full access