0
0
Swiftprogramming~10 mins

Omitting argument labels with _ in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the function without using the argument label.

Swift
func greet(_ name: String) {
    print("Hello, \(name)!")
}
greet([1])
Drag options to blanks, or click blank then click option'
Alabel: "Alice"
Bgreet: "Alice"
Cname: "Alice"
D"Alice"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the argument label when the function expects it to be omitted.
Trying to write the label explicitly even though it's omitted.
2fill in blank
medium

Complete the function call by omitting the argument label as required.

Swift
func multiply(_ a: Int, by b: Int) -> Int {
    return a * b
}
let result = multiply([1], by: 5)
Drag options to blanks, or click blank then click option'
A3
Ba: 3
Cby: 3
Db: 3
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the label 'a:' when the function expects it to be omitted.
Confusing the labels for the parameters.
3fill in blank
hard

Fix the error by correctly calling the function that omits the first argument label.

Swift
func divide(_ numerator: Double, by denominator: Double) -> Double {
    return numerator / denominator
}
let quotient = divide([1], by: 2.0)
Drag options to blanks, or click blank then click option'
Anumerator: 10.0
B10.0
Cby: 10.0
Ddenominator: 10.0
Attempts:
3 left
💡 Hint
Common Mistakes
Including the label 'numerator:' causes a compile error.
Mixing up the labels for numerator and denominator.
4fill in blank
hard

Fill both blanks to define and call a function that omits the first argument label.

Swift
func power([1] base: Int, exponent: Int) -> Int {
    var result = 1
    for _ in 1...exponent {
        result *= base
    }
    return result
}
let value = power([2], exponent: 4)
Drag options to blanks, or click blank then click option'
A_
Bbase
Cexponent
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the underscore in the function definition.
Using the label when calling the function for the parameter with underscore.
5fill in blank
hard

Fill all three blanks to define and call a function with two omitted argument labels.

Swift
func concatenate([1] first: String, [2] second: String, separator: String) -> String {
    return first + separator + second
}
let text = concatenate([3], "World", separator: ", ")
Drag options to blanks, or click blank then click option'
A_
Bfirst
Csecond
D"Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Including argument labels for parameters that use underscore.
Not using underscore in the function definition.