Exploring functions
Functions are useful for encapsulating several instructions that collectively perform a specific task, for example:
- Calculating the 10% service charge for a meal at a restaurant
- Calculating the monthly payment for a car that you wish to purchase
Here’s what a function looks like:
func functionName(parameter1: ParameterType, ...) -> ReturnType {
code
}
Every function has a descriptive name. You can define one or more values that the function takes as input, known as parameters. You can also define what the function will output when done, known as its return type. Both parameters and return types are optional.
You “call” a function’s name to execute it. This is what a function call looks like:
functionName(parameter1: argument1, …)
You provide input values (known as arguments) that match the type of the function’s parameters.
To learn more about functions, visit...