Member-only story
Intro to Functions in Swift
Functions in Swift Series
- Intro to Functions in Swift (this article)
- Argument Labels
Functions in Swift — Introduction
This post is going to explain what a function is and how to use it. It is a basic look at the parts of a function. More detail will be given in other posts.
Topics covered in this post
What is a Function
According to the Swift documentation, “functions are self-contained chunks of code that perform a specific task.”
Usually a function is defined when the task needs to be repeated or used somewhere else.
How to define a function
When defining a function, it needs to start with func
. This tells the swift compiler that the next chunk of code is going to be a function. Then it needs an argument name followed by parentheses. Inside the parentheses, is where parameters go.
Function parameters are not required for a function. Parameters are necessary when the task needs data not accessible by the function. All parameters in a function must have a unique name. When using the parameters in the function, the parameter name is used to access the value it contains.