Recall & Review
beginner
What is a tuple in Swift?
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: <br>
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.Click to reveal answer
What does this Swift function return?<br>
func example() -> (Int, String) { return (5, "hello") }✗ Incorrect
The function returns a tuple with two values: an Int (5) and a String ("hello").
How do you access the second element of a tuple returned by a function in Swift?
✗ Incorrect
Tuple elements can be accessed by their position using dot and index, like result.1, or by name if named.
Which of these is a valid tuple return type in Swift?
✗ Incorrect
Tuples are defined with parentheses and can have named elements, like (name: String, age: Int).
Can a Swift function return multiple values without using tuples?
✗ Incorrect
Functions can return a custom struct or class to hold multiple values, but tuples are a simpler way.
What is the benefit of naming elements in a tuple returned by a function?
✗ Incorrect
Naming tuple elements helps you access them clearly by name, improving code readability.
Explain how to create a Swift function that returns a tuple with named elements and how to access those elements.
Think about the function signature and using dot notation to get values.
You got /3 concepts.
Describe why you might choose to return a tuple from a function instead of a single value or a custom type.
Consider convenience and simplicity.
You got /4 concepts.