Let's say you want to do the following:
- Calculate the 10% service charge for a meal at a restaurant.
- Calculate the monthly payment for a car that you wish to purchase.
You would use a function for this.
A function contains a number of instructions that perform a specific task. You give it a name and call it by this name when needed. You can also define what it takes as input and what it outputs. Function inputs are called parameters, and the function output is called the return type.
Here's what a function looks like:
func functionName(paramater1: parameterType, ...) -> returnType {
code
}
Both parameters and return types are optional. You can have more than one parameter, but you can only have one value returned.
Let's see how to create a function.