Value Semantics
Inout Update
An inout parameter makes mutation explicit at the call site.
Pass a value for update
inout_update.swift
func addBonus(to score: inout Int, bonus: Int) {
score += bonus
}
let bonus =
var score = 10
addBonus(to: &score, bonus: bonus)
let message = "score=\(score)"
print(message)
func addBonus(to score: inout Int, bonus: Int) {
score += bonus
}
let bonus =
var score = 10
addBonus(to: &score, bonus: bonus)
let message = "score=\(score)"
print(message)
func addBonus(to score: inout Int, bonus: Int) {
score += bonus
}
let bonus =
var score = 10
addBonus(to: &score, bonus: bonus)
let message = "score=\(score)"
print(message)
inout
The `&` marker shows where a function is allowed to update the caller's variable.