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.

Like what you read? Give Andrew Sowers a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.