I know about the ampersand as a bit operation but sometimes I see it in front of variable names. What does putting an &
in front of variables do?
If you put &
before a variable in a function, that means this variable is inout variable.
@Icaro already described what it means, I will just give an example to illustrate the difference between inout variables and in variables:
func majec(inout xValue:Int, var yValue:Int) {
xValue = 100
yValue = 200
}
var xValue = 33
var yValue = 33
majec(&xValue, yValue: yValue)
xValue //100
yValue //33