← 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.
Syntax
swift
func functionName(label name: Type) { }     // custom label
func functionName(_ name: Type) { }         // no label

functionName(label: value)
functionName(value)

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
// Define the function `multiply` taking `x`, `y` and returning an Int
func multiply(x: Int, y: Int) -> Int {
    // Return `x * y` from this function
    return x * y
}
// Print `multiply(x: 6, y: 7)` to the console
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
// Define the function `greet` taking `name` and returning a String
func greet(person name: String) -> String {
    // Return `"Hello, \(name)"` from this function
    return "Hello, \(name)"
}
// Print `greet(person: "Alice")` to the console
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
// Define the function `square` taking `number` and returning an Int
func square(_ number: Int) -> Int {
    // Return `number * number` from this function
    return number * number
}
// Print `square(5)` to the console
print(square(5))
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.