9.12 Working with In-Out Parameters
When a variable is passed through as a parameter to a function, we now know that the parameter is treated as a constant within the body of that function. We also know that if we want to make changes to a parameter value we have to create a shadow copy as outlined in the above section. Since this is a copy, any changes made to the variable are not, by default, reflected in the original variable. Consider, for example, the following code:
var myValue = 10
func doubleValue (_ value: Int) -> Int {
var value = value
value += value
return(value)
}
print("Before function call myValue = \(myValue)")
print("doubleValue call returns \(doubleValue(myValue))")
print("After function call myValue = \(myValue)")
The code begins by declaring a variable named myValue initialized with a value...