Notice: Undefined index: understanding-swift in /var/sites/hackingwithswift.com/site/src/Controller/NewBooksReadController.php on line 89
Why does Swift use parameter labels? - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Why does Swift use parameter labels?

Paul Hudson    @twostraws   

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.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Link copied to your pasteboard.