WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Why does Swift use parameter labels?

Paul Hudson    @twostraws   

Updated for Xcode 14.2

Many programming languages either do not use parameter labels or make them optional. Swift is unusual in that it uses them extensively, and it even lets us differentiate between external parameter names and internal parameter names – it’s doubly unusual!

To look at why labels are useful, consider code like this:

setReactorStatus(true, true, false)

That’s perfectly normal code in many languages, but you will rarely if ever see that in Swift because we prefer to give our parameters names. So, we’d write our function like this:

func setReactorStatus(primaryActive: Bool, backupActive: Bool, isEmergency: Bool) {
    // code here
}

As a result, Swift will require those Booleans to be used when calling the function, like this:

setReactorStatus(primaryActive: true, backupActive: true, isEmergency: false)

And now our code becomes much clearer – we know exactly what each value does when we call the function, rather than having to remember whether isEmergency comes first or last.

Of course, Swift takes this even further by letting us write our labels twice, like this:

func setAge(for person: String, to value: Int) {
    print("\(person) is now \(value)")
}

This solves two problems at once: it makes those labels more meaningful inside the function, and also makes those labels more meaningful outside the function.

To demonstrate this, here’s how the function is called right now:

setAge(for: "Paul", to: 40)

If we had only used the person and value labels, we would have to write this instead:

setAge(person: "Paul", value: 40)

I hope you can see that the original code can actually be read aloud as a standard English statement: “set age for Paul to 40”. On the other hand, “set age person Paul value 40” isn’t natural at all.

Going the other way, if we had used only the for and to labels, then calling the function would look natural but using those values inside the function would be weird:

func setAge(for: String, to: Int) {
    print("\(for) is now \(to)")
}

In fact, Swift wouldn’t even allow that because it will think for is the start of a loop!

By having both the internal and external labels, our functions read more naturally both where we call them and inside the function itself. They aren’t required, and often you’ll have only one label for your parameters, but it’s still nice to have them around.

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.7/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.