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.
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
return output
}
Let's look at this in more detail to see how a function is defined:
- func: This indicates that you are declaring a function.
- nameOfFunction: This will be the name of your function and, by convention, is written in camel case (this means that each word, apart from...