Inout Parameters
suggest changeFunctions can modify the parameters passed to them if they are marked with the inout keyword. When passing an inout parameter to a function, the caller must add a & to the variable being passed.
func updateFruit(fruit: inout Int) {
fruit -= 1
}
var apples = 30
print("There's \(apples) apples") // Prints "There's 30 apples"
updateFruit(fruit: &apples)
print("There's now \(apples) apples") // Prints "There's now 29 apples".
The new value is written back to the caller's variable, so a value type can be updated in place. inout is copy-in copy-out, which is different from giving the value reference semantics.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents