Swift InOut Parameters

Modifying function parameters directly

🔄 What are InOut Parameters?

InOut parameters allow functions to modify the original values passed to them. By default, Swift parameters are constants, but inout makes them modifiable references to the original variables.


// Basic inout parameter example
func doubleValue(number: inout Int) {
    number *= 2
}

var myNumber = 5
doubleValue(number: &myNumber)
print(myNumber) // Output: 10
                                    

Key InOut Concepts

🔄

Reference Passing

Pass variables by reference, not value

func swap(a: inout Int, b: inout Int) {
    let temp = a
    a = b
    b = temp
}

Ampersand Operator

Use & when calling inout functions

var x = 10, y = 20
swap(a: &x, b: &y)
// x is now 20, y is now 10
📝

Variable Only

Only variables can be passed as inout

var mutableVar = 5
doubleValue(number: &mutableVar) // ✅ Works

// doubleValue(number: &10) // ❌ Error
🚫

No Constants

Constants cannot be inout parameters

let constantValue = 5
// doubleValue(number: &constantValue) // ❌ Error
// Constants are immutable

🔹 Basic InOut Example

Here's how to modify a variable using inout parameters:

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

var counter = 0
print("Before: \(counter)")  // Before: 0

increment(value: &counter)
print("After: \(counter)")   // After: 1

Output:

Before: 0

After: 1

🔹 Swapping Values

A common use case for inout parameters is swapping values:

func swapStrings(first: inout String, second: inout String) {
    let temporary = first
    first = second
    second = temporary
}

var greeting = "Hello"
var farewell = "Goodbye"

print("Before swap: \(greeting), \(farewell)")
swapStrings(first: &greeting, second: &farewell)
print("After swap: \(greeting), \(farewell)")

Output:

Before swap: Hello, Goodbye

After swap: Goodbye, Hello

🔹 Array Modification

InOut parameters work great with arrays and other collections:

func addElement(to array: inout [Int], element: Int) {
    array.append(element)
}

func removeFirst(from array: inout [String]) {
    if !array.isEmpty {
        array.removeFirst()
    }
}

var numbers = [1, 2, 3]
var names = ["Alice", "Bob", "Charlie"]

addElement(to: &numbers, element: 4)
removeFirst(from: &names)

print("Numbers: \(numbers)")  // [1, 2, 3, 4]
print("Names: \(names)")      // ["Bob", "Charlie"]

Output:

Numbers: [1, 2, 3, 4]

Names: ["Bob", "Charlie"]

🔹 Important Rules

Remember these key rules when using inout parameters:

✅ Do:

  • Use & when calling functions with inout parameters
  • Pass only variables (not constants or literals)
  • Use inout when you need to modify the original value
  • Consider inout for performance with large data structures

❌ Don't:

  • Pass constants or literal values to inout parameters
  • Use inout unnecessarily - prefer return values when possible
  • Forget the & operator when calling the function

🧠 Test Your Knowledge

What operator do you use when calling a function with inout parameters?