Bird
Raised Fist0
Swiftprogramming~10 mins

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

Choose your learning style10 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 '&'.

Practice

(1/5)
1.

What does the inout keyword do in Swift functions?

easy
A. Allows the function to modify the original variable passed in.
B. Prevents the function from changing the variable.
C. Creates a copy of the variable that cannot be changed.
D. Makes the variable constant inside the function.

Solution

  1. Step 1: Understand the role of inout

    The inout keyword allows a function to receive a variable and modify it directly.
  2. Step 2: Effect on the original variable

    Changes made inside the function affect the original variable outside the function.
  3. Final Answer:

    Allows the function to modify the original variable passed in. -> Option A
  4. Quick Check:

    inout means modify original variable [OK]
Hint: Remember: inout means function can change your variable [OK]
Common Mistakes:
  • Thinking inout creates a copy instead of modifying original
  • Forgetting to use & when calling the function
  • Assuming variables are constant inside the function
2.

Which of the following is the correct way to declare a function with an inout parameter in Swift?

func updateValue(____ value: inout Int) { value += 10 }
easy
A. func updateValue(inout value: Int)
B. func updateValue(value: inout Int)
C. func updateValue(value inout Int)
D. func updateValue(inout value in Int)

Solution

  1. Step 1: Recall Swift syntax for inout parameters

    The correct syntax places inout before the type, after the parameter name: paramName: inout Type.
  2. Step 2: Match the options

    func updateValue(value: inout Int) matches this syntax exactly: value: inout Int.
  3. Final Answer:

    func updateValue(value: inout Int) -> Option B
  4. Quick Check:

    Parameter syntax = name: inout Type [OK]
Hint: Remember: inout goes after parameter name, before type [OK]
Common Mistakes:
  • Placing inout before parameter name
  • Swapping parameter name and type order
  • Using invalid syntax like 'inout value in Int'
3.

What will be the output of this Swift code?

func doubleValue(_ number: inout Int) {
    number *= 2
}

var myNum = 5
doubleValue(&myNum)
print(myNum)
medium
A. 5
B. Error: missing & in function call
C. 10
D. Error: cannot modify constant

Solution

  1. Step 1: Understand the function behavior

    The function doubles the value of the passed variable by multiplying it by 2.
  2. Step 2: Trace the code execution

    Variable myNum starts at 5, is passed with & to allow mutation, so after the function call it becomes 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    In-out parameter doubles 5 to 10 [OK]
Hint: Use & when calling to let function change variable [OK]
Common Mistakes:
  • Forgetting & when calling function
  • Expecting original value unchanged
  • Confusing parameter passing with copy
4.

Identify the error in this Swift code snippet:

func increment(_ value: inout Int) {
    value += 1
}

let number = 10
increment(&number)
medium
A. Cannot pass a constant as an inout parameter.
B. Missing & when calling the function.
C. Function parameter should not be inout.
D. Function must return the new value.

Solution

  1. Step 1: Check variable mutability

    The variable number is declared with let, making it a constant.
  2. Step 2: Understand inout parameter requirements

    Inout parameters require a variable that can be modified, so constants cannot be passed.
  3. Final Answer:

    Cannot pass a constant as an inout parameter. -> Option A
  4. Quick Check:

    Inout needs variable, not constant [OK]
Hint: Only variables (var), not constants (let), can be inout [OK]
Common Mistakes:
  • Trying to pass constants as inout parameters
  • Forgetting & when calling function
  • Thinking function must return value when using inout
5.

You want to write a function swapValues that swaps two integers using inout parameters. Which of the following implementations correctly swaps the values?

func swapValues(_ a: inout Int, _ b: inout Int) {
    ____
}
hard
A. let temp = a temp = b b = temp
B. a = b b = a
C. a = b b = a - b a = a - b
D. var temp = a a = b b = temp

Solution

  1. Step 1: Understand swapping logic

    To swap two values, you need a temporary variable to hold one value while assigning the other.
  2. Step 2: Check each option

    var temp = a a = b b = temp correctly declares a temporary variable with var and swaps values safely. let temp = a temp = b b = temp uses let which is immutable and causes error. a = b b = a overwrites a before assigning b. a = b b = a - b a = a - b uses arithmetic swap but is more complex and not recommended here.
  3. Final Answer:

    var temp = a a = b b = temp -> Option D
  4. Quick Check:

    Use temp variable with var to swap values [OK]
Hint: Use a temporary variable declared with var to swap values [OK]
Common Mistakes:
  • Using let for temporary variable causing immutability error
  • Overwriting one variable before saving its value
  • Trying to swap without a temporary variable incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard