0
0
Swiftprogramming~10 mins

In-out parameters for mutation in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - In-out parameters for mutation
Call function with variable
Function receives variable as in-out
Function modifies variable
Changes reflect back to original variable
Function returns
Original variable now updated
The function receives a variable reference, modifies it inside, and the changes update the original variable outside the function.
Execution Sample
Swift
func increment(value: inout Int) {
    value += 1
}

var number = 5
increment(value: &number)
print(number)
This code increases the variable 'number' by 1 using an in-out parameter.
Execution Table
StepActionVariable 'number' ValueFunction Parameter 'value'Output
1Initialize number5N/AN/A
2Call increment with &number5Reference to number (5)N/A
3Inside function: value += 156 (value incremented)N/A
4Function returns, changes apply to number6N/AN/A
5Print number6N/A6
💡 Function ends, and the original variable 'number' is updated to 6 because of in-out parameter mutation.
Variable Tracker
VariableStartAfter CallInside FunctionAfter ReturnFinal
number556 (via value)66
value (parameter)N/A5 (ref to number)6N/AN/A
Key Moments - 3 Insights
Why do we use '&' when passing 'number' to the function?
The '&' symbol tells Swift to pass the variable by reference so the function can modify the original variable. See execution_table step 2 where '&number' is passed.
Does the function get a copy or the original variable?
With 'inout', the function gets a reference to the original variable, not a copy. Changes inside the function affect the original. See execution_table steps 3 and 4.
What happens if we don't use 'inout' but try to modify the parameter?
Without 'inout', the parameter is a copy and changes inside the function do not affect the original variable. Here, the mutation would not reflect outside the function.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'number' immediately after the function call but before the function body executes?
A5
B6
CN/A
D0
💡 Hint
Check execution_table row 2 and 3 for 'number' value before and after function body.
At which step does the original variable 'number' get updated?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at execution_table step 4 where changes apply back to 'number'.
If we remove '&' when calling increment, what will happen to 'number'?
AIt will increase by 1
BCompilation error
CIt will stay the same
DIt will become zero
💡 Hint
Swift requires '&' for inout parameters; see key_moments about '&' usage.
Concept Snapshot
In-out parameters let functions modify variables passed in.
Use 'inout' in function and '&' when calling.
Changes inside function update original variable.
Without 'inout', parameters are copies.
Useful for mutation without return values.
Full Transcript
This example shows how Swift functions can change variables outside their scope using in-out parameters. The function 'increment' takes an 'inout Int' parameter, meaning it can modify the original variable passed in. When calling the function, we use '&' before the variable name to pass it by reference. Inside the function, the value is increased by 1. After the function returns, the original variable 'number' reflects this change. This is different from normal parameters, which are copies and do not affect the original variable. The execution table traces each step, showing how 'number' starts at 5, is passed by reference, incremented inside the function, and finally updated to 6 outside. Key moments clarify why '&' is needed and how mutation works. The quiz tests understanding of these steps and consequences of missing '&'.