0
0
Swiftprogramming~10 mins

Argument labels and parameter names 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 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'
Aat
Bwith
Cfor
Dto
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.
2fill in blank
medium

Complete 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'
Ato
Bfor
Cwith
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the parameter name instead of the argument label.
Omitting the label entirely.
3fill in blank
hard

Fix 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'
A_
Bwith
Cby
Dfor
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.
4fill in blank
hard

Fill 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'
Aby
B_
Cwith
Dfrom
Attempts:
3 left
💡 Hint
Common Mistakes
Using '_' which removes labels and makes calls less clear.
Using labels that don't match the division concept.
5fill in blank
hard

Fill 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'
A_
Bwith
Cand
Dof
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.