Swiftly

Swift 5.7 references for busy coders

In-out parameters

In-out parameters are function parameters whose values can be modified, with those modifications persisting after the function ends. They enable the sharing of value types as if they were reference types.

Function with an inout parameter

func haveBirthday(age: inout Int) {
  print("Happy birthday!")
  age += 1
}

Passing in an inout parameter with &

var myAge = 50
print("I am \(myAge).")

haveBirthday(age: &myAge)
print("Now I am \(myAge).")

// Output:
// I am 50.
// Happy birthday!
// Now I am 51.

Further reading