Functions
In Swift, a function is a self-contained block of code that performs a specific task. Functions are generally used to logically break our code into reusable named blocks. We use the function's name to call
the function.
When we define a function, we can also optionally define one or more parameters (also known as arguments). Parameters are named values that are passed into the function by the code that calls it. These parameters are generally used within the function to perform the task of the function. We can also define default values for the parameters to simplify how they are called.
Every Swift function has a type associated with it which is referred to as the return type. It defines the type of data returned from the function to the code that called it. If a value is not returned from a function, the return type is void
.
Let's look at how to define functions in Swift.
Using a single parameter function
The syntax used to define a function in Swift is very flexible. This...