Reusing code in functions
Functions are a building block of almost all programming languages, allowing functionality to be defined and reused.
Swift’s syntax provides an expressive way to define your functions, creating concise and readable code.
In this recipe, we will run through the different types of functions we can create and understand how to define and use them.
Getting ready
In this recipe, we can use the playground from the previous recipe. Don’t worry if you didn’t work through the previous recipe, as this one will contain all the code you need.
How to do it…
Let’s look at how functions are defined in Swift:
func nameOfFunction(parameterLabel1 parameter1: ParameterType1, parameterLabel2 parameter2: ParameterType2,...) -> OutputType { // Function's implementation // If the function has an output type, // the function must return a valid value...