Tin

Examples · learn/11-values · 171 of 339

inout

examples/learn/11-values/inout.tintin run examples/learn/11-values/inout.tin
type Robot = { name: string, battery: int }

// inout: this function may change the caller's variable.
fn charge(r: inout Robot, amount: int) {
  r.battery = min(100, r.battery + amount)
}

fn main() {
  var bud = Robot { name: "Bud", battery: 70 }
  charge(&bud, 20)         // & at the call: you can see that bud may change here
  print(bud)
  charge(&bud, 20)
  print(bud)
}