Bird
0
0

Find the error in this Swift code:

medium📝 Debug Q7 of 15
Swift - Functions
Find the error in this Swift code:
func greet(_ person: String, from hometown: String) {
    print("Hello \(person)! Glad you came from \(hometown).")
}
greet("John", "New York")
ANo error, code runs fine
BUnderscore used incorrectly for first parameter
CFunction print statement syntax error
DMissing argument label 'from' in function call
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter labels

    First parameter uses _ so no label needed; second parameter requires label from.
  2. Step 2: Analyze function call

    Calling greet("John", "New York") misses the required label from: for second parameter.
  3. Final Answer:

    Missing argument label 'from' in function call -> Option D
  4. Quick Check:

    Underscore omits first label; second label required [OK]
Quick Trick: Labels must match unless _ is used [OK]
Common Mistakes:
  • Omitting required labels
  • Assuming _ omits all labels
  • Incorrect function call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes