Bird
0
0

Which of the following function declarations correctly omits the argument label for the first parameter?

easy📝 Syntax Q12 of 15
Swift - Functions
Which of the following function declarations correctly omits the argument label for the first parameter?
Afunc greet(_name: String) { print("Hello, \(_name)!") }
Bfunc greet(_ name: String) { print("Hello, \(name)!") }
Cfunc greet(name _ : String) { print("Hello, \(name)!") }
Dfunc greet(name: String) { print("Hello, \(name)!") }
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct underscore usage

    The underscore must appear before the parameter name with a space, like _ name: String.
  2. Step 2: Check each option

    func greet(_ name: String) { print("Hello, \(name)!") } uses _ name: String correctly. func greet(name: String) { print("Hello, \(name)!") } uses a label. func greet(name _ : String) { print("Hello, \(name)!") } has wrong syntax. func greet(_name: String) { print("Hello, \(_name)!") } uses an invalid parameter name.
  3. Final Answer:

    func greet(_ name: String) { print("Hello, \(name)!") } -> Option B
  4. Quick Check:

    Correct underscore syntax = func greet(_ name: String) { print("Hello, \(name)!") } [OK]
Quick Trick: Underscore must be alone before parameter name [OK]
Common Mistakes:
  • Putting underscore after parameter name
  • Adding spaces between underscore and name incorrectly
  • Using underscore as part of parameter name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes