0
0
Swiftprogramming~10 mins

Omitting argument labels with _ in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Omitting argument labels with _
Define function with _ before parameter
Call function without argument label
Function receives argument
Execute function body
Return or print result
This flow shows how a function defined with _ before a parameter allows calling it without using the argument label.
Execution Sample
Swift
func greet(_ name: String) {
    print("Hello, \(name)!")
}

greet("Alice")
Defines a function greet that takes a name without requiring an argument label, then prints a greeting.
Execution Table
StepActionInput/ParameterOutput/Print
1Define function greet with _ for parametername: String (no label)
2Call greet function"Alice"
3Function greet receives argumentname = "Alice"
4Print greetingHello, Alice!
5Function ends
💡 Function completes after printing greeting.
Variable Tracker
VariableStartAfter CallFinal
nameundefined"Alice""Alice"
Key Moments - 2 Insights
Why can we call greet("Alice") without using a label?
Because the function parameter uses _ to omit the argument label, so the call does not require it (see execution_table step 2).
What happens if we try to call greet(name: "Alice")?
It causes an error because the parameter label is omitted with _, so the call must not use a label (refer to execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name' at step 3?
A"Alice"
Bundefined
C"Bob"
Dnil
💡 Hint
Check the 'Input/Parameter' column at step 3 in the execution_table.
At which step does the function print the greeting?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Output/Print' column in the execution_table.
If we remove the _ in the function definition, how would the call change?
ACall changes to greet(_: "Alice")
BCall stays greet("Alice")
CCall changes to greet(name: "Alice")
DCall changes to greet()
💡 Hint
Without _, argument labels are required in Swift function calls.
Concept Snapshot
func functionName(_ parameterName: Type) {
  // code
}

- The _ before parameterName means no label needed when calling.
- Call with functionName(value), not functionName(parameterName: value).
- Useful for cleaner calls when label adds no clarity.
Full Transcript
This visual execution shows how to define a Swift function that omits argument labels by using an underscore before the parameter name. The function greet is defined with _ name: String, so when calling greet, you just pass the argument without a label, like greet("Alice"). The execution table traces defining the function, calling it with "Alice", receiving the argument, printing the greeting, and ending the function. The variable tracker shows the parameter 'name' changes from undefined to "Alice" after the call. Key moments clarify why the label is omitted and what happens if you try to use a label. The quiz tests understanding of parameter values, print timing, and how calls change if _ is removed. This helps beginners see step-by-step how omitting argument labels works in Swift.