Inout Parameters

suggest change

Functions 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents