0
0
Swiftprogramming~10 mins

Functions returning tuples in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Functions returning tuples
Call function
Function runs code
Create tuple with values
Return tuple to caller
Use tuple values
The function runs, creates a tuple with multiple values, returns it, and the caller uses those values.
Execution Sample
Swift
func getUser() -> (name: String, age: Int) {
    return ("Alice", 30)
}

let user = getUser()
print(user.name, user.age)
This function returns a tuple with a name and age, which we then print.
Execution Table
StepActionEvaluationResult
1Call getUser()Function startsFunction running
2Return tuple ("Alice", 30)Tuple created(name: "Alice", age: 30)
3Assign tuple to useruser = (name: "Alice", age: 30)user holds tuple
4Print user.name and user.ageAccess tuple elementsOutput: Alice 30
5EndNo more codeProgram ends
💡 Function returns tuple and program finishes printing values.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
usernilnil(name: "Alice", age: 30)(name: "Alice", age: 30)
Key Moments - 3 Insights
Why do we use parentheses around multiple return values?
Parentheses group multiple values into one tuple, as shown in step 2 where the tuple ("Alice", 30) is created and returned.
How do we access individual values from the returned tuple?
We use dot notation with the tuple's named elements, like user.name and user.age in step 4.
What happens if the function returns without a tuple?
The code expects a tuple, so returning a single value or no value would cause an error; step 2 shows the correct tuple return.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'user' after step 3?
Anil
B"Alice"
C(name: "Alice", age: 30)
D30
💡 Hint
Check the variable_tracker table under 'After Step 3' for 'user'.
At which step does the function create the tuple to return?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the execution_table row where the tuple is created and returned.
If the tuple elements were unnamed, how would you access the age value?
Auser.1
Buser.age
Cuser.name
Duser[0]
💡 Hint
Unnamed tuples use index starting at 0; see how named elements are accessed in step 4.
Concept Snapshot
func functionName() -> (type1, type2) {
  return (value1, value2)
}

- Functions can return multiple values as a tuple.
- Use parentheses to group values.
- Access tuple elements by name or index.
Full Transcript
This example shows how a Swift function can return multiple values grouped as a tuple. The function getUser returns a tuple with a name and age. When called, the function runs and creates the tuple ("Alice", 30). This tuple is returned to the caller and assigned to the variable user. We then access the tuple's elements using dot notation, printing the name and age. The execution table traces each step from calling the function to printing the values. The variable tracker shows how the user variable changes from nil to holding the tuple. Key moments clarify why parentheses are needed, how to access tuple elements, and the importance of returning the correct tuple. The quiz tests understanding of variable values at steps, when the tuple is created, and how to access unnamed tuple elements.