0
0
Swiftprogramming~15 mins

Omitting argument labels with _ in Swift - Deep Dive

Choose your learning style9 modes available
Overview - Omitting argument labels with _
What is it?
In Swift, functions often have argument labels to clarify what each parameter means when calling the function. Sometimes, you want to skip writing these labels when calling the function for simplicity or readability. Using an underscore (_) before a parameter name tells Swift to omit the argument label when calling the function. This means you just pass the value directly without naming it.
Why it matters
Argument labels help make code clearer, but sometimes they can be verbose or unnecessary. Omitting them with _ lets you write cleaner, shorter calls when the meaning is obvious. Without this feature, Swift code could become cluttered with repetitive labels, making it harder to read and write, especially for simple functions or those with common parameters.
Where it fits
Before learning this, you should understand basic Swift functions and how argument labels work. After this, you can explore advanced function features like default parameters, variadic parameters, and function overloading to write flexible and expressive Swift code.
Mental Model
Core Idea
Using _ before a parameter name tells Swift to skip the argument label when calling the function, making calls simpler and cleaner.
Think of it like...
It's like when you call a friend by their first name only because you both know who you mean, instead of saying their full name every time.
Function definition with label:
func greet(person name: String) {
  print("Hello, \(name)!")
}
Call: greet(person: "Anna")

Function definition omitting label:
func greet(_ name: String) {
  print("Hello, \(name)!")
}
Call: greet("Anna")
Build-Up - 6 Steps
1
FoundationUnderstanding Swift function parameters
šŸ¤”
Concept: Functions in Swift can have argument labels and parameter names to clarify calls.
In Swift, a function parameter can have two names: an argument label used when calling the function, and a parameter name used inside the function. For example: func greet(person name: String) { print("Hello, \(name)!") } Here, 'person' is the argument label, and 'name' is the parameter name.
Result
You call this function as greet(person: "Anna"), which prints Hello, Anna!
Knowing the difference between argument labels and parameter names helps you understand how Swift functions communicate clearly.
2
FoundationCalling functions with argument labels
šŸ¤”
Concept: Argument labels make function calls more readable by naming each argument explicitly.
When you call a function with argument labels, you write the label before the value: greet(person: "Anna") This shows clearly what the argument means, improving code readability.
Result
The function prints Hello, Anna! when called with the label.
Argument labels act like signposts in code, making it easier to understand what each value represents.
3
IntermediateUsing _ to omit argument labels
šŸ¤”Before reading on: do you think using _ means you must still write the argument label when calling the function? Commit to your answer.
Concept: Placing _ before a parameter name tells Swift to skip the argument label in calls.
You can write a function like this: func greet(_ name: String) { print("Hello, \(name)!") } Now, when calling greet, you just write: greet("Anna") No label is needed before the argument.
Result
The function prints Hello, Anna! without requiring the label in the call.
Understanding that _ removes the label simplifies function calls and lets you write cleaner code when labels add no extra clarity.
4
IntermediateMixing labeled and unlabeled parameters
šŸ¤”Before reading on: can a function have some parameters with labels and others without? Predict yes or no.
Concept: Functions can mix parameters with and without argument labels using _ selectively.
Example: func bookFlight(from origin: String, _ destination: String) { print("Flying from \(origin) to \(destination)") } Call: bookFlight(from: "NYC", "LA") Here, 'from' is labeled, but the destination parameter uses _ so no label is needed.
Result
Prints: Flying from NYC to LA
Knowing you can mix labeled and unlabeled parameters gives you fine control over function call clarity and brevity.
5
AdvancedDefault behavior without explicit _
šŸ¤”Before reading on: do you think Swift always requires argument labels unless _ is used? Commit your guess.
Concept: By default, Swift requires argument labels for all parameters except the first one, unless _ is used to override this.
In Swift, the first parameter usually has no label by default, but subsequent parameters do. For example: func add(_ a: Int, to b: Int) -> Int { return a + b } Call: add(3, to: 5) // works If you want to omit the label 'to', you use _: func add(_ a: Int, _ b: Int) -> Int { return a + b } Call: add(3, 5) // works without labels
Result
Understanding this default helps you predict when labels are needed and when they are not.
Knowing Swift's default labeling rules prevents confusion and helps you design functions with the right call style.
6
ExpertImpact on API design and readability
šŸ¤”Before reading on: do you think omitting argument labels always improves code readability? Commit your answer.
Concept: Choosing when to omit argument labels affects how clear or confusing your API is for users.
In professional Swift code, argument labels act like documentation. Omitting them with _ can make calls shorter but less explicit. For example: func move(_ x: Int, _ y: Int) {} vs func move(toX x: Int, toY y: Int) {} The first is shorter but less clear; the second is more descriptive. Experienced developers balance brevity and clarity depending on context.
Result
Good API design uses _ thoughtfully to keep code clean without losing meaning.
Understanding the tradeoff between clarity and brevity helps you write APIs that are both easy to use and understand.
Under the Hood
Swift's compiler uses the underscore _ as a special marker to tell the function call syntax parser to skip expecting an argument label for that parameter. Internally, the function still has a parameter name for use inside the function body, but the call site syntax omits the label. This is handled at compile time, so calls without labels are translated correctly to the function's parameter list.
Why designed this way?
Swift was designed to improve code readability by using argument labels, but also to allow flexibility. The _ syntax was introduced to let developers decide when labels add value or just clutter calls. This design balances clarity with conciseness, unlike some older languages that either always require labels or never use them.
Function definition
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│ func example(_ param1: Int, param2: Int) │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
              │
Call site    │
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”“ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│ example(5, param2: 10)       │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Compiler interprets _ as no label needed for param1, but param2 requires label.
Myth Busters - 3 Common Misconceptions
Quick: Does using _ mean the parameter has no name inside the function? Commit yes or no.
Common Belief:Using _ means the parameter has no name inside the function body.
Tap to reveal reality
Reality:The parameter still has a name inside the function; _ only affects the call site, not the internal parameter name.
Why it matters:Confusing this leads to errors when trying to use the parameter inside the function, causing compilation failures.
Quick: If you omit all argument labels with _, can you still call the function with labels? Commit yes or no.
Common Belief:If all labels are omitted with _, you can still call the function using argument labels.
Tap to reveal reality
Reality:Once _ is used to omit a label, you cannot use that label in calls; the call must omit it.
Why it matters:Trying to use labels when they are omitted causes compiler errors and confusion.
Quick: Does omitting argument labels always make code easier to read? Commit yes or no.
Common Belief:Omitting argument labels always makes code simpler and easier to read.
Tap to reveal reality
Reality:Sometimes omitting labels makes calls ambiguous or confusing, especially with multiple parameters of the same type.
Why it matters:Poorly designed APIs without labels can lead to misunderstandings and bugs in large codebases.
Expert Zone
1
Using _ can affect Swift's automatic documentation generation, as labels often appear in docs to explain parameters.
2
When overriding methods, the use of _ must match the superclass method's parameter labels exactly, or the override will fail.
3
Swift's function type signatures include argument labels, so omitting labels changes the function's type and affects function references.
When NOT to use
Avoid omitting argument labels when parameters have similar types or when clarity is critical. Instead, use descriptive labels or default parameter values to improve readability.
Production Patterns
In production Swift code, _ is often used in initializers or simple utility functions where labels add noise. Framework APIs balance labels and omissions carefully to create intuitive and concise interfaces.
Connections
Named parameters in Python
Both use labels to clarify arguments, but Python allows optional naming at call time, while Swift requires explicit labels unless omitted with _.
Understanding Swift's _ helps appreciate different language designs for balancing clarity and brevity in function calls.
API design principles
Omitting argument labels relates to designing clear and usable APIs by controlling how much information is shown at call sites.
Knowing when to omit labels teaches broader lessons about user-friendly interface design beyond programming.
Human communication clarity
Omitting labels is like choosing when to use formal titles or nicknames in conversation to balance politeness and brevity.
This connection shows how programming language features mirror everyday communication choices about clarity and efficiency.
Common Pitfalls
#1Trying to use an argument label that was omitted with _.
Wrong approach:func greet(_ name: String) {} greet(name: "Anna") // Error: Unexpected argument label 'name:'
Correct approach:func greet(_ name: String) {} greet("Anna") // Correct call without label
Root cause:Misunderstanding that _ removes the label from the call site, so the label cannot be used.
#2Omitting labels for multiple parameters of the same type causing confusion.
Wrong approach:func move(_ x: Int, _ y: Int) {} move(10, 20) // Which is x and which is y?
Correct approach:func move(toX x: Int, toY y: Int) {} move(toX: 10, toY: 20) // Clear which value is which
Root cause:Ignoring the importance of labels for clarity when parameters are similar.
#3Using _ inconsistently in overridden methods causing override failures.
Wrong approach:class Base { func greet(_ name: String) {} } class Child: Base { override func greet(name: String) {} // Error: Method does not override
Correct approach:class Child: Base { override func greet(_ name: String) {} // Correct override }
Root cause:Not matching argument label usage exactly in subclass overrides.
Key Takeaways
In Swift, _ before a parameter name means you omit the argument label when calling the function.
Argument labels improve code clarity, but _ lets you write shorter calls when labels are unnecessary.
The parameter still has a name inside the function; _ only affects how you call it.
Use _ carefully to balance readability and brevity, especially in public APIs.
Misusing or misunderstanding _ can cause compiler errors or confusing code.