Swift in-out variables — passing by reference

Andrew Sowers
Jan 19, 2016 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store