0
0
Swiftprogramming~10 mins

Argument labels and parameter names in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Argument labels and parameter names
Define function with labels and names
Call function with argument labels
Match labels to parameters
Execute function body using parameter names
Return or print result
End
This flow shows how Swift uses argument labels in calls to match parameters in function definitions, then runs the function body.
Execution Sample
Swift
func greet(person name: String, from hometown: String) {
    print("Hello \(name)! Glad you could visit from \(hometown).")
}
greet(person: "Anna", from: "Paris")
Defines a function with argument labels and parameter names, then calls it with matching labels.
Execution Table
StepActionArgument LabelParameter NameValueOutput
1Define function-person (name), from (hometown)--
2Call functionpersonname"Anna"-
3Call functionfromhometown"Paris"-
4Execute print---Hello Anna! Glad you could visit from Paris.
5End----
💡 Function completes after printing greeting message.
Variable Tracker
VariableStartAfter Call
name-"Anna"
hometown-"Paris"
Key Moments - 2 Insights
Why do we use argument labels like 'person' and 'from' instead of just parameter names?
Argument labels clarify the meaning when calling the function, as shown in execution_table rows 2 and 3 where labels 'person' and 'from' match parameters 'name' and 'hometown'.
Can the argument label and parameter name be different?
Yes, as in the example, the label 'person' corresponds to parameter 'name'. This helps make calls readable while keeping parameter names descriptive inside the function (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value does the parameter 'name' have at step 2?
A"person"
B"Anna"
C"Paris"
DNo value yet
💡 Hint
Check the 'Parameter Name' and 'Value' columns at step 2 in the execution_table.
At which step does the function print the greeting message?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in the execution_table to find when the message appears.
If we swapped the argument labels in the call, what would happen?
AThe function would print the same message.
BThe function would print a message with swapped names.
CThe code would not compile due to label mismatch.
DThe function would ignore the labels and use parameter order.
💡 Hint
Swift requires argument labels to match function definition; see steps 2 and 3 in execution_table.
Concept Snapshot
Swift functions can have argument labels and parameter names.
Argument labels are used when calling the function.
Parameter names are used inside the function body.
Labels can differ from parameter names for clarity.
Calls must use correct argument labels to match parameters.
Full Transcript
This visual execution shows how Swift uses argument labels and parameter names in functions. First, the function is defined with labels 'person' and 'from' and parameters 'name' and 'hometown'. When calling the function, the argument labels 'person' and 'from' are used to pass values 'Anna' and 'Paris'. The function matches these labels to parameters and executes the print statement using parameter names. The output is the greeting message. This helps make function calls clear and readable while keeping parameter names descriptive inside the function.