0
0
Swiftprogramming~10 mins

Functions returning tuples in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function that returns a tuple with two integers.

Swift
func getCoordinates() -> [1] {
    return (10, 20)
}
Drag options to blanks, or click blank then click option'
A(Int, Int)
BString
CInt
DVoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using just Int as return type instead of a tuple.
Using String as return type when the function returns numbers.
2fill in blank
medium

Complete the code to call the function and store the returned tuple.

Swift
let position: [1] = getCoordinates()
Drag options to blanks, or click blank then click option'
A(Int, Int)
BInt
CString
DDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using Int instead of a tuple type for the variable.
Using String or Double which do not match the tuple.
3fill in blank
hard

Fix the error in the function return type to correctly return a tuple with named elements.

Swift
func getUser() -> [1] {
    return (name: "Alice", age: 30)
}
Drag options to blanks, or click blank then click option'
A(String, Int)
B(name: String, age: Int)
CString
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using unnamed tuple type (String, Int) when names are present.
Using just String or Int as return type.
4fill in blank
hard

Fill both blanks to extract the tuple elements into variables.

Swift
let user = getUser()
let [1] = user.name
let [2] = user.age
Drag options to blanks, or click blank then click option'
AuserName
BuserAge
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same names as tuple elements which can cause confusion.
Using unrelated variable names.
5fill in blank
hard

Fill all three blanks to define a function returning a tuple and call it correctly.

Swift
func [1]() -> (x: Int, y: Int) {
    return (x: 5, y: 10)
}

let point = [2]()
print("x is \(point.[3])")
Drag options to blanks, or click blank then click option'
AmakePoint
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching function name and call.
Accessing the wrong tuple element.