We’ve reached the final day of the first week of the 100 Days of SwiftUI. Yesterday, we learned about loops and how to use them to iterate over code. Today, we’re learning about functions, another essential part of SwiftUI. Let’s dive in!
Reusing code with functions in SwiftUI
A lot of times as a developer, you want to reuse the code you’ve written in various parts of your program. You could copy and paste your code, but that would be a bit messy. For example, if you want to change your code in one place, you’d also have to manually change all the other instances. That’s where a function comes in.
You write a function just once, give it an easy and logical name and then call the function in various parts of your program. You can also pass parameters to further extend the functionality of your function.
// Creates a function
func myFunction() {
print("This is my function!")
}
myFunction()
// This will print "This is my function!"
// Creates a function with parameters
func simpleCalculator(firstNumber: Int, secondNumber: Int) {
print("\(firstNumber) + \(secondNumber) = \(firstNumber + secondNumber)")
}
simpleCalculator(firstNumber: 5, secondNumber: 7)
Returning a value from a function
A function can also be used to return a value that can be used outside off that function. This could be anything, a String, an int, a boolean, etc. The most important thing when using a function to return a value, is that you declare what type of data you’re going to return and actually return it. If you forget either of them, your code will not run. Swift just won’t allow it!
// Function that returns a boolean
func checkIfEven(number: Int) -> Bool {
if number % 2 == 0 {
return true
} else {
return false
}
}
print(checkIfEven(number: 22))
// Function where the return is extracted from the code
func checkIfEven(number: Int) -> Bool {
number % 2 == 0
}
print(checkIfEven(number: 22))
Returning multiple values from a function
A function can also be used to return multiple values. To accomplish this, we could make use of various data types that store multiple values, like arrays or sets. In this example, however, we’re going to be using a tuple. If you want to know when to use an array, a set or a tuple in SwiftUI, check out this article by Paul.
func getDriver() -> (driverName: String, driverTeam: String) {
(driverName: "Max Verstappen", driverTeam: "Red Bull Racing")
}
let (driverName, driverTeam) = getDriver()
print("\(driverName) \(driverTeam)")
// Prints "Max Verstappen Red Bull Racing
Customising parameter labels
Writing clear labels for parameters is vital for any developer. Just think about it:
- You come back to your code after a few months. Clearly named parameters will quickly help you understand how they’re implemented.
- A fellow developer may need to use your code or look it over. A generic or non-telling name makes their job much harder, as they’ll need so spend more time analysing and understanding your code.
However, there are also times that we might want to omit a parameter name. If you interested, check out this article by Paul.
func isCat(catName name: String) -> Bool {
(name == "Persian" || name == "Meowth")
}
var catResult = isCat(catName: "Meowth")
print(catResult)
And that was it for day 7! We’re not done with functions just yet though, so we’ll be returning to them tomorrow. Stay tuned and let me know what you think of today in the comments below!
100 Days of SwiftUI – Day 7 – Functions