Menu
Coddy logo textTech

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: 25

Without 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 icon

Challenge

Easy

Create two functions that demonstrate different argument label techniques:

Function 1: send

  • Use the argument label message with parameter name text (type String)
  • Use the argument label to with parameter name recipient (type String)
  • Return a string in the format: "Sending '[text]' to [recipient]"

Function 2: double

  • Use an underscore _ to omit the argument label
  • Parameter name number (type Int)
  • 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
14

Cheat 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: 25

Without 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 results
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals