Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The underscore (_) in the function parameter means you omit the argument label when calling the function. So just pass the value directly.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the label 'a:' when the function expects it to be omitted.
Confusing the labels for the parameters.
✗ Incorrect
The first parameter uses _ so you omit the label and just pass the value directly.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Including the label 'numerator:' causes a compile error.
Mixing up the labels for numerator and denominator.
✗ Incorrect
The first parameter uses _ so you must omit the label and pass only the value.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Using _ before 'base' omits the argument label, so you call the function passing just the value for 'base'.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Including argument labels for parameters that use underscore.
Not using underscore in the function definition.
✗ Incorrect
Using _ before 'first' and 'second' omits their argument labels, so you call the function passing values directly for those parameters.