How can I deal with this error without creating additional variable?
func reduceToZero(x:Int) -> Int {
while (x != 0) {
x = x-1 //
In Swift you just add the var keyword before the variable name in the function declaration:
func reduceToZero(var x:Int) -> Int { // notice the "var" keyword
while (x != 0) {
x = x-1
}
return x
}
Refer to the subsection "Constant and Variable Parameters" in the "Functions" chapter of the Swift book (page 210 of the iBook as it is today).