Swift Functions
This tutorial belongs to the Swift series
Your program’s code is organized into functions.
A function is declared using the func keyword:
func bark() {
print("woof!")
}
Functions can be assigned to structures, classes and enumerations, and in this case we call them methods.
A function is invoked using its name:
bark()
A function can return a value:
func bark() -> String {
print("woof!")
return "barked successfully"
}
And you can assign it to a variable:
let result = bark()
A function can accept parameters. Each parameter has a name, and a type:
func bark(times: Int) {
for index in 0..<times {
print("woof!")
}
}
The name of a parameter is internal to the function.
We use the name of the parameter when we call the function, to pass in its value:
bark(times: 3)
When we call the function we must pass all the parameters defined.
Here is a function that accepts multiple parameters:
func bark(times: Int, repeatBark: Bool) {
for index in 0..<times {
if repeatBark == true {
print("woof woof!")
} else {
print("woof!")
}
}
}
In this case you call it in this way:
bark(times: 3, repeat: true)
When we talk about this function, we don’t call it bark(). We call it bark(times:repeat:).
This is because we can have multiple functions with the same name, but different set of parameters.
You can avoid using labels by using the _ keyword:
func bark(_ times: Int, repeatBark: Bool) {
//...the function body
}
So you can invoke it in this way:
bark(3, repeat: true)
It’s common in Swift and iOS APIs to have the first parameter with no label, and the other parameters labeled.
It makes for a nice and expressive API, when you design the names of the function and the parameters nicely.
You can only return one value from a function. If you need to return multiple values, it’s common to return a tuple:
func bark() -> (String, Int) {
print("woof!")
return ("barked successfully", 1)
}
And you can assign the result to a tuple:
let (result, num) = bark()
print(result) //"barked successfully"
print(num) //1
Functions can be nested inside other functions. When this happens, the inner function is invisible to outside the outer function.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.