Bird
0
0

Identify the error in this Swift function returning a tuple:

medium📝 Debug Q14 of 15
Swift - Functions
Identify the error in this Swift function returning a tuple:
func divide(_ a: Int, _ b: Int) -> (quotient: Int, remainder: Int) {
    let q = a / b
    let r = a % b
    return q, r
}
AReturn statement syntax is incorrect for tuple
BDivision by zero error possible
CTuple elements must be unnamed
DFunction parameters must be named
Step-by-Step Solution
Solution:
  1. Step 1: Check return statement syntax

    Returning multiple values as a tuple requires parentheses: return (q, r), not return q, r.
  2. Step 2: Confirm tuple element names are allowed

    Named tuple elements in return type are valid; parameters can be unnamed as shown.
  3. Final Answer:

    Return statement syntax is incorrect for tuple -> Option A
  4. Quick Check:

    Return tuple with parentheses [OK]
Quick Trick: Use parentheses around tuple values in return [OK]
Common Mistakes:
  • Omitting parentheses in return tuple
  • Assuming parameters must be named
  • Ignoring possible division by zero (not asked here)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes