Challenge - 5 Problems
Swift Argument Labels Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function call with argument labels
What is the output of this Swift code?
Swift
func greet(person name: String) { print("Hello, \(name)!") } greet(person: "Anna")
Attempts:
2 left
💡 Hint
Check how the argument label and parameter name are used in the function call.
✗ Incorrect
The function greet uses 'person' as the argument label and 'name' as the parameter name. Calling greet(person: "Anna") passes "Anna" to the parameter 'name', so it prints "Hello, Anna!".
❓ Predict Output
intermediate2:00remaining
Result of function with omitted argument label
What will this Swift code print?
Swift
func multiply(_ a: Int, by b: Int) -> Int { return a * b } print(multiply(3, by: 4))
Attempts:
2 left
💡 Hint
The underscore (_) means the first argument label is omitted.
✗ Incorrect
The first parameter has an underscore, so no label is needed when calling. The second parameter requires the label 'by'. The call multiply(3, by: 4) returns 12.
🔧 Debug
advanced2:00remaining
Identify the error in function call with argument labels
Why does this Swift code cause an error?
Swift
func divide(_ numerator: Int, denominator: Int) -> Int { return numerator / denominator } divide(numerator: 10, denominator: 2)
Attempts:
2 left
💡 Hint
Check the function definition and how the first parameter is called.
✗ Incorrect
The first parameter has an underscore, so it must be called without a label. Calling divide(numerator: 10, denominator: 2) causes an error because 'numerator:' label is unexpected.
❓ Predict Output
advanced2:00remaining
Output of function with different argument labels and parameter names
What does this Swift code print?
Swift
func describe(person name: String, from city: String) { print("\(name) is from \(city).") } describe(person: "Liam", from: "Paris")
Attempts:
2 left
💡 Hint
Look at how argument labels and parameter names are used in the function call.
✗ Incorrect
The function uses 'person' and 'from' as argument labels, and 'name' and 'city' as parameter names. The call matches the labels, so it prints 'Liam is from Paris.'.
🧠 Conceptual
expert2:00remaining
Understanding argument labels and parameter names in Swift
Which statement about argument labels and parameter names in Swift is correct?
Attempts:
2 left
💡 Hint
Think about the difference between how you call a function and how you write its code.
✗ Incorrect
In Swift, argument labels are used when calling a function to clarify the meaning of each argument. Parameter names are used inside the function body to refer to those arguments.