Argument Labels
Part of the Fundamentals section of Coddy's Swift journey — lesson 54 of 86.
Swift gives you control over how function calls read by using argument labels. An argument label is what you use when calling the function, while the parameter name is what you use inside the function body.
You can specify both by writing the argument label before the parameter name:
func greet(to name: String) {
print("Hello, \(name)!")
}
greet(to: "Alice") // Output: Hello, Alice!Here, to is the argument label (used when calling), and name is the parameter name (used inside the function). This makes the call read more naturally: "greet to Alice."
Sometimes you want to omit the label entirely for cleaner calls. Use an underscore (_) as the argument label:
func square(_ number: Int) -> Int {
return number * number
}
print(square(5)) // Output: 25Without the underscore, you'd have to write square(number: 5). The underscore removes that requirement, making the call simpler when the function name already makes the purpose clear.
Challenge
EasyCreate two functions that demonstrate different argument label techniques:
Function 1: send
- Use the argument label
messagewith parameter nametext(typeString) - Use the argument label
towith parameter namerecipient(typeString) - Return a string in the format:
"Sending '[text]' to [recipient]"
Function 2: double
- Use an underscore
_to omit the argument label - Parameter name
number(typeInt) - Return the number multiplied by 2
After defining both functions, read two strings and one integer from input, then call both functions and print the results on separate lines.
You will receive the following inputs:
- First line: the message text (e.g.,
"Hello") - Second line: the recipient name (e.g.,
"Alice") - Third line: a number to double (e.g.,
"7")
For example, if the inputs are "Hello", "Alice", and "7", the output should be:
Sending 'Hello' to Alice
14Cheat sheet
Swift allows you to control how function calls read by using argument labels. An argument label is used when calling the function, while the parameter name is used inside the function body.
Specify both by writing the argument label before the parameter name:
func greet(to name: String) {
print("Hello, \(name)!")
}
greet(to: "Alice") // Output: Hello, Alice!Here, to is the argument label and name is the parameter name.
To omit the argument label for cleaner calls, use an underscore (_):
func square(_ number: Int) -> Int {
return number * number
}
print(square(5)) // Output: 25Without the underscore, you would need to write square(number: 5).
Try it yourself
// Read input
let messageText = readLine()!
let recipientName = readLine()!
let numberToDouble = Int(readLine()!)!
// TODO: Write your code below
// Define the 'send' function with argument labels 'message' and 'to'
// Define the 'double' function with omitted argument label using '_'
// Call both functions and print the resultsThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input