0
0
Swiftprogramming~20 mins

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

Choose your learning style9 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.