A tuple is a group of multiple values combined into a single compound value. Each value can be of any type, and tuples can hold different types together.
Click to reveal answer
beginner
How do you define a function in Swift that returns a tuple?
You specify the return type as a tuple in the function signature. For example: func getCoordinates() -> (x: Int, y: Int) { return (x: 10, y: 20) }
Click to reveal answer
beginner
How do you access values from a tuple returned by a function?
You can access tuple elements by name or by position. For example, if a function returns (x: 10, y: 20), you can use result.x or result.0.
Click to reveal answer
beginner
Can Swift functions return tuples with different types inside?
Yes! Tuples can hold values of different types. For example, a function can return (name: String, age: Int, isStudent: Bool).
Click to reveal answer
beginner
Why use tuples as return types in Swift functions?
Tuples let you return multiple related values from a function without creating a custom type. This is useful for simple grouped data like coordinates or multiple results.
Hint: Use colon between name and type in tuple [OK]
Common Mistakes:
Using type before name in tuple elements
Separating tuple elements with semicolon instead of comma
Omitting names or using wrong punctuation
3. What is the output of this Swift code?
func minMax(numbers: [Int]) -> (min: Int, max: Int) {
var currentMin = numbers[0]
var currentMax = numbers[0]
for number in numbers[1...] {
if number < currentMin { currentMin = number }
if number > currentMax { currentMax = number }
}
return (currentMin, currentMax)
}
let result = minMax(numbers: [3, 7, 2, 9, 4])
print(result.min, result.max)
medium
A. 2 9
B. 3 7
C. 9 2
D. Error: tuple elements not accessible
Solution
Step 1: Trace min and max calculation
Start with first number 3 as min and max. Iterate over 7, 2, 9, 4 updating min to 2 and max to 9.
Step 2: Access tuple elements by name
Returned tuple has named elements min and max, so result.min is 2 and result.max is 9.
Final Answer:
2 9 -> Option A
Quick Check:
min = 2, max = 9 [OK]
Hint: Tuple elements accessed by name like properties [OK]
Common Mistakes:
Confusing min and max values
Trying to access tuple elements by index instead of name
Forgetting to start loop from second element
4. 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
}
medium
A. Return statement syntax is incorrect for tuple
B. Division by zero error possible
C. Tuple elements must be unnamed
D. Function parameters must be named
Solution
Step 1: Check return statement syntax
Returning multiple values as a tuple requires parentheses: return (q, r), not return q, r.
Step 2: Confirm tuple element names are allowed
Named tuple elements in return type are valid; parameters can be unnamed as shown.
Final Answer:
Return statement syntax is incorrect for tuple -> Option A
Quick Check:
Return tuple with parentheses [OK]
Hint: 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)
5. You want to write a Swift function that returns the sum, average, and count of an array of Ints as a tuple with named elements. Which of these implementations correctly does this?
hard
A. func stats(numbers: [Int]) -> (Int, Int, Int) {
let sum = numbers.reduce(0, +)
let count = numbers.count
let avg = sum / count
return (sum, avg, count)
}
B. func stats(numbers: [Int]) -> (sum: Int, avg: Int, count: Int) {
let sum = numbers.reduce(0, +)
let count = numbers.count
let avg = sum / count
return (sum, avg, count)
}
C. func stats(numbers: [Int]) -> (sum: Int, avg: Double, count: Int) {
let sum = numbers.reduce(0, +)
let count = numbers.count
let avg = Double(sum) / Double(count)
return (sum, avg, count)
}
D. func stats(numbers: [Int]) -> (sum: Int, avg: Double, count: Int) {
let sum = numbers.reduce(0, +)
let count = numbers.count
let avg = sum / count
return (sum, avg, count)
}
Solution
Step 1: Check return type matches values
Return type must be named tuple (sum: Int, avg: Double, count: Int). Correct implementation declares this exactly.
Step 2: Verify average calculation
Average requires Double conversion: Double(sum) / Double(count) to get precise floating-point result, avoiding integer truncation.
Step 3: Confirm return statement matches tuple
return (sum, avg, count) aligns with named elements in order and type.
Final Answer:
func stats(numbers: [Int]) -> (sum: Int, avg: Double, count: Int) {
let sum = numbers.reduce(0, +)
let count = numbers.count
let avg = Double(sum) / Double(count)
return (sum, avg, count)
} -> Option C
Quick Check:
named tuple + Double(sum)/Double(count) [OK]
Hint: Match tuple element types exactly, convert for average [OK]
Common Mistakes:
Using Int for average causing integer division
Mismatching tuple element names and return values
Omitting type conversion for average calculation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.