Bird
0
0

Find the error in this Swift function:

medium📝 Debug Q7 of 15
Swift - Functions
Find the error in this Swift function:
func printNumbers(_ nums: Int..., extra: Int) {
    for n in nums {
        print(n)
    }
    print(extra)
}
ANo error, function is valid
BCannot have more than one parameter
CVariadic parameter must be the last parameter
DMissing return type
Step-by-Step Solution
Solution:
  1. Step 1: Recall Swift variadic parameter rules

    Variadic parameter must be the last parameter in the function declaration.
  2. Step 2: Analyze function parameters

    Here, variadic parameter is first, followed by 'extra', which is invalid.
  3. Final Answer:

    Variadic parameter must be the last parameter -> Option C
  4. Quick Check:

    Variadic always last parameter [OK]
Quick Trick: Variadic parameter must be last in parameter list [OK]
Common Mistakes:
  • Placing variadic parameter before others
  • Thinking multiple parameters are disallowed
  • Ignoring parameter order rules

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes