Swift in-out variables — passing by reference

Passing by reference is sometimes convenient depending on your needs. Luckily, if you’re using Swift, it’s pretty simple.

In Swift, you can add the inout keyword in place of var. Here’s an example:

var myInt:Int = 5

func doubleInt(inout myInt:Int) -> Void {
myInt *= 2
}

print(myInt) // 5

doubleInt(&myInt)

print(myInt) // 10

Originally published at Haiku Robot.

Show your support

Clapping shows how much you appreciated Andrew Sowers’s story.