Bird
0
0

Identify the error in this Swift function declaration:

medium📝 Debug Q6 of 15
Swift - Functions
Identify the error in this Swift function declaration:
func add(numbers: Int...) -> Int {
    return numbers.reduce(0, +)
}
add(1, 2, 3)
AMissing underscore before parameter name in call
BCannot use reduce on variadic parameters
CNo error, function works correctly
DVariadic parameter must be last in parameter list
Step-by-Step Solution
Solution:
  1. Step 1: Check function call syntax

    The function parameter has an external name 'numbers', so call must use it or use underscore in declaration.
  2. Step 2: Identify call error

    Calling add(1, 2, 3) without 'numbers:' causes error; correct call is add(numbers: 1, 2, 3) or declare with underscore.
  3. Final Answer:

    Missing underscore before parameter name in call -> Option A
  4. Quick Check:

    Parameter name required unless underscore used [OK]
Quick Trick: Use _ to omit external parameter name or include it in call [OK]
Common Mistakes:
  • Assuming variadic cannot use reduce
  • Ignoring external parameter names
  • Thinking variadic must be last always (true but not error here)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes