Updated for Xcode 14.2
Adding parameters to functions lets us add customization points, so that functions can operate on different data depending on our needs. Sometimes we want to make these customization points available to keep our code flexible, but other times you don’t want to think about it – you want the same thing nine times out of ten.
For example, previously we looked at this function:
func printTimesTables(for number: Int, end: Int) {
for i in 1...end {
print("\(i) x \(number) is \(i * number)")
}
}
printTimesTables(for: 5, end: 20)
That prints any times table, starting at 1 times the number up to any end point. That number is always going to change based on what multiplication table we want, but the end point seems like a great place to provide a sensible default – we might want to count up to 10 or 12 most of the time, while still leaving open the possibility of going to a different value some of the time.
To solve this problem, Swift lets us specify default values for any or all of our parameters. In this case, we could set end
to have the default value of 12, meaning that if we don’t specify it 12 will be used automatically.
Here’s how that looks in code:
func printTimesTables(for number: Int, end: Int = 12) {
for i in 1...end {
print("\(i) x \(number) is \(i * number)")
}
}
printTimesTables(for: 5, end: 20)
printTimesTables(for: 8)
Notice how we can now call printTimesTables()
in two different ways: we can provide both parameters for times when we want it, but if we don’t – if we just write printTimesTables(for: 8)
– then the default value of 12 will be used for end
.
We’ve actually seen a default parameter in action, back in some code we used before:
var characters = ["Lana", "Pam", "Ray", "Sterling"]
print(characters.count)
characters.removeAll()
print(characters.count)
That adds some strings to an array, prints its count, then removes them all and prints the count again.
As a performance optimization, Swift gives arrays just enough memory to hold their items plus a little extra so they can grow a little over time. If more items are added to the array, Swift allocates more and more memory automatically, so that as little as possible is wasted.
When we call removeAll()
, Swift will automatically remove all the items in the array, then free up all the memory that was assigned to the array. That’s usually what you’ll want, because after all you’re removing the objects for a reason. But sometimes – just sometimes – you might be about to add lots of new items back into the array, and so there’s a second form of this function that removes the items while also keeping the previous capacity:
characters.removeAll(keepingCapacity: true)
This is accomplished using a default parameter value: keepingCapacity
is a Boolean with the default value of false so that it does the sensible thing by default, while also leaving open the option of us passing in true
for times we want to keep the array’s existing capacity.
As you can see, default parameter values let us keep flexibility in our functions without making them annoying to call most of the time – you only need to send in some parameters when you need something unusual.
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.