Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function with an argument label.
Swift
func greet([1] name: String) { print("Hello, \(name)!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the parameter name directly without an argument label.
Choosing a label that doesn't make sense for greeting.
✗ Incorrect
The argument label 'to' is used before the parameter name 'name' to call the function like greet(to: "Anna").
2fill in blank
mediumComplete the code to call the function using the correct argument label.
Swift
greet([1]: "Sam")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the parameter name instead of the argument label.
Omitting the label entirely.
✗ Incorrect
The function was defined with the argument label 'to', so the call must use greet(to: "Sam").
3fill in blank
hardFix the error in the function definition by adding the correct argument label.
Swift
func multiply([1] number: Int, by multiplier: Int) -> Int { return number * multiplier }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '_' which removes the argument label but breaks the call style.
Using a label that doesn't match the function call.
✗ Incorrect
The argument label 'by' is used before the parameter 'number' to match the function call style multiply(by: 3, by: 4).
4fill in blank
hardFill both blanks to define a function with argument labels for both parameters.
Swift
func divide([1] dividend: Int, [2] divisor: Int) -> Int { return dividend / divisor }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '_' which removes labels and makes calls less clear.
Using labels that don't match the division concept.
✗ Incorrect
The argument labels 'from' and 'by' make the function call read naturally: divide(from: 10, by: 2).
5fill in blank
hardFill all three blanks to define a function with argument labels and parameter names.
Swift
func calculateArea([1] width: Double, [2] height: Double, [3] unit: String) -> String { let area = width * height return "Area is \(area) \(unit)²" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding labels to the first parameter which is usually unlabeled.
Using labels that don't fit the meaning of the parameters.
✗ Incorrect
Using '_' for width means no label needed, 'with' for height and 'of' for unit make the call read: calculateArea(5.0, with: 10.0, of: "meters").