← Back to Swift Course | Chapter 4: Functions | Lesson 2 of 7

Argument Labels

Argument labels are the friendly words you type before a value when calling a function, so the call reads like a sentence.

Default Argument Labels

By default, calling a function requires using each parameter's name as a label, which makes the call self-documenting.

Example: Default Argument Labels

markup
func multiply(x: Int, y: Int) -> Int {
    return x * y
}
print(multiply(x: 6, y: 7))

Custom External Labels

You can give a parameter a different label for callers than the name used inside the function body, improving readability at the call site.

Note: Here person is what callers type, while name is used inside the function body.

Example: Custom External Labels

markup
func greet(person name: String) -> String {
    return "Hello, \(name)"
}
print(greet(person: "Alice"))

Omitting Labels with Underscore

Using an underscore _ as the external label allows the function to be called without naming that particular argument.

Example: Omitting Labels with Underscore

markup
func square(_ number: Int) -> Int {
    return number * number
}
print(square(5))
Common Mistakes
  1. Confusing the argument label (used at the call site) with the parameter name (used inside the function body) -- they can be different words.
  2. Forgetting that using _ as the label removes the requirement to write a label at all when calling.
  3. Assuming all Swift functions require labels; the first parameter's label is often omitted by convention when it reads naturally without one.
Chapter Summary
  • By default, a parameter's name serves as both its external label and internal name.
  • You can specify a different external label before the internal name: func f(externalLabel internalName: Type).
  • Using _ as the external label lets the function be called without naming that argument.
  • Good argument labels make call sites read like natural English.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.