Swift make method parameter mutable?

As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a pointer.

func reduceToZero(_ x: inout Int) {
    while (x != 0) {
        x = x-1     

var a = 3
print(a) // will print '0'

This can be particularly useful in recursion.

Apple’s inout declaration guidelines can be found here.

Leave a Comment