Bird
Raised Fist0
Swiftprogramming~20 mins

Omitting argument labels with _ in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Argument Label Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code?

Consider the following Swift function and call:

func greet(_ name: String) {
    print("Hello, \(name)!")
}
greet("Alice")

What will this print?

Swift
func greet(_ name: String) {
    print("Hello, \(name)!")
}
greet("Alice")
AHello, _Alice!
BError: Missing argument label
CHello, Alice!
DHello, name!
Attempts:
2 left
💡 Hint

Check how the underscore (_) affects the argument label when calling the function.

❓ Predict Output
intermediate
2:00remaining
What error does this code produce?

Given this Swift function:

func multiply(_ a: Int, b: Int) -> Int {
    return a * b
}
multiply(a: 3, b: 4)

What error will occur when compiling?

Swift
func multiply(_ a: Int, b: Int) -> Int {
    return a * b
}
multiply(a: 3, b: 4)
AError: Extraneous argument label 'a:' in call
BError: Missing argument label 'a:' in call
C12
DError: Missing argument label 'b:' in call
Attempts:
2 left
💡 Hint

Look at which parameters have labels and which don't.

🔧 Debug
advanced
2:00remaining
Why does this function call fail?

Examine this Swift code:

func divide(_ numerator: Int, _ denominator: Int) -> Int {
    return numerator / denominator
}
divide(numerator: 10, denominator: 2)

Why does the call to divide fail?

Swift
func divide(_ numerator: Int, _ denominator: Int) -> Int {
    return numerator / denominator
}
divide(numerator: 10, denominator: 2)
ABecause the function parameters are constants and cannot be used in division
BBecause the function parameters require argument labels, but the call omits them
CBecause the function returns an Int but the call expects a Double
DBecause the function parameters omit argument labels, but the call uses labels
Attempts:
2 left
💡 Hint

Check the use of underscores (_) in the function parameters and how the function is called.

🧠 Conceptual
advanced
2:00remaining
How does the underscore (_) affect argument labels in Swift functions?

Which statement best describes the effect of using an underscore (_) before a parameter name in a Swift function?

AIt omits the argument label when calling the function, so only the value is passed.
BIt makes the argument label mandatory when calling the function.
CIt makes the parameter optional and allows nil values.
DIt automatically assigns a default value to the parameter.
Attempts:
2 left
💡 Hint

Think about how you call functions with and without argument labels.

❓ Predict Output
expert
2:00remaining
What is the output of this Swift code with multiple underscores?

Analyze this Swift code:

func calculate(_ x: Int, _ y: Int, multiplier: Int) -> Int {
    return (x + y) * multiplier
}

let result = calculate(2, 3, multiplier: 4)
print(result)

What will be printed?

Swift
func calculate(_ x: Int, _ y: Int, multiplier: Int) -> Int {
    return (x + y) * multiplier
}

let result = calculate(2, 3, multiplier: 4)
print(result)
AError: Missing argument label for multiplier
B20
CError: Missing argument label for first or second parameter
D10
Attempts:
2 left
💡 Hint

Check which parameters require labels and which do not.

Practice

(1/5)
1. What does the underscore _ do when used before a function parameter in Swift?
easy
A. It changes the parameter type to Any.
B. It makes the parameter optional.
C. It makes the parameter a constant inside the function.
D. It allows you to call the function without writing the argument label.

Solution

  1. Step 1: Understand the role of underscore in parameters

    The underscore before a parameter name tells Swift to omit the argument label when calling the function.
  2. Step 2: Effect on function call

    This means you can pass the argument directly without naming it, making the call shorter and simpler.
  3. Final Answer:

    It allows you to call the function without writing the argument label. -> Option D
  4. Quick Check:

    Underscore = omit argument label [OK]
Hint: Underscore means skip argument label when calling [OK]
Common Mistakes:
  • Thinking underscore makes parameter optional
  • Confusing underscore with type changes
  • Assuming underscore affects parameter mutability
2. Which of the following function declarations correctly omits the argument label for the first parameter?
easy
A. func greet(_name: String) { print("Hello, \(_name)!") }
B. func greet(_ name: String) { print("Hello, \(name)!") }
C. func greet(name _ : String) { print("Hello, \(name)!") }
D. func greet(name: String) { print("Hello, \(name)!") }

Solution

  1. Step 1: Identify correct underscore usage

    The underscore must appear before the parameter name with a space, like _ name: String.
  2. Step 2: Check each option

    func greet(_ name: String) { print("Hello, \(name)!") } uses _ name: String correctly. func greet(name: String) { print("Hello, \(name)!") } uses a label. func greet(name _ : String) { print("Hello, \(name)!") } has wrong syntax. func greet(_name: String) { print("Hello, \(_name)!") } uses an invalid parameter name.
  3. Final Answer:

    func greet(_ name: String) { print("Hello, \(name)!") } -> Option B
  4. Quick Check:

    Correct underscore syntax = func greet(_ name: String) { print("Hello, \(name)!") } [OK]
Hint: Underscore must be alone before parameter name [OK]
Common Mistakes:
  • Putting underscore after parameter name
  • Adding spaces between underscore and name incorrectly
  • Using underscore as part of parameter name
3. What is the output of this Swift code?
func multiply(_ a: Int, _ b: Int) -> Int {
    return a * b
}

print(multiply(3, 4))
medium
A. 12
B. multiply(3, 4)
C. Error: Missing argument labels
D. 7

Solution

  1. Step 1: Understand function parameters with underscore

    Both parameters have underscores, so argument labels are omitted when calling.
  2. Step 2: Calculate the function result

    Calling multiply(3, 4) returns 3 * 4 = 12, which is printed.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Underscore means call without labels, so multiply(3,4) works [OK]
Common Mistakes:
  • Expecting argument labels in call
  • Confusing output with string of function call
  • Adding parameters incorrectly
4. Identify the error in this Swift function call:
func divide(_ numerator: Double, denominator: Double) -> Double {
    return numerator / denominator
}

divide(10, denominator: 2)
medium
A. Cannot mix omitted and required labels in parameters
B. Missing argument label for denominator
C. No error, code runs fine
D. Missing argument label for numerator

Solution

  1. Step 1: Analyze parameter labels

    The first parameter uses underscore, so no label needed. The second requires label denominator.
  2. Step 2: Check function call

    The call uses divide(10, denominator: 2), which matches the parameter labels correctly.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Underscore omits first label, second label required [OK]
Hint: First param no label, second param label required [OK]
Common Mistakes:
  • Expecting both labels omitted
  • Trying to omit second label incorrectly
  • Confusing parameter order
5. You want to write a Swift function formatDate that takes a Date and a String format. You want to call it like formatDate(myDate, "yyyy-MM-dd") without argument labels. Which function declaration is correct?
hard
A. func formatDate(_ date: Date, _ format: String) -> String { /* implementation */ }
B. func formatDate(date: Date, format: String) -> String { /* implementation */ }
C. func formatDate(_ date: Date, format: String) -> String { /* implementation */ }
D. func formatDate(date: Date, _ format: String) -> String { /* implementation */ }

Solution

  1. Step 1: Understand call without labels

    Calling formatDate(myDate, "yyyy-MM-dd") means both parameters must omit labels.
  2. Step 2: Match function declaration

    Only func formatDate(_ date: Date, _ format: String) -> String { /* implementation */ } uses underscore for both parameters, allowing call without labels.
  3. Final Answer:

    func formatDate(_ date: Date, _ format: String) -> String { /* implementation */ } -> Option A
  4. Quick Check:

    Both params underscore = no labels in call [OK]
Hint: Use underscore for all params to omit all labels [OK]
Common Mistakes:
  • Omitting only one label but calling without both
  • Mixing labeled and unlabeled parameters incorrectly
  • Forgetting underscore syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard