Swift make method parameter mutable?

SwiftSyntaxImmutabilityMutability

Swift Problem Overview


How can I deal with this error without creating additional variable?

func reduceToZero(x:Int) -> Int {
    while (x != 0) {
        x = x-1            // ERROR: cannot assign to 'let' value 'x'
    }
    return x
}

I don't want to create additional variable just to store the value of x. Is it even possible to do what I want?

Swift Solutions


Solution 1 - Swift

As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a pointer.

func reduceToZero(_ x: inout Int) {
    while (x != 0) {
        x = x-1     
    }
}

var a = 3
reduceToZero(&a)
print(a) // will print '0'

This can be particularly useful in recursion.

Apple's inout declaration guidelines can be found here.

Solution 2 - Swift

'var' parameters are deprecated and will be removed in Swift 3. So assigning to a new parameter seems like the best way now:

func reduceToZero(x:Int) -> Int {
    var x = x
    while (x != 0) {
        x = x-1            
    }
    return x
}

as mentioned here: https://stackoverflow.com/questions/36164973/var-parameters-are-deprecated-and-will-be-removed-in-swift-3

Solution 3 - Swift

For Swift 1 and 2 (for Swift 3 see answer by achi using an inout parameter): Argument of a function in Swift is let by default so change it to var if you need to alter the value i.e,

func reduceToZero(var x:Int) -> Int {
    while (x != 0) {
        x = x-1     
    }
    return x
}

Solution 4 - Swift

Swift3 answer for passing mutable array pointer.

Function:

func foo(array: inout Array<Int>) {
    array.append(1)
}

Call to function:

var a = Array<Int>()
foo(array:&a)

Solution 5 - Swift

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).

Solution 6 - Swift

There are some cases where we dont ned to use inout

We can use something like this if you want that changes/scope to be only inside the function:

func manipulateData(a: Int) -> Int {
    var a = a
    // ...
}

Solution 7 - Swift

Solution using Swift5 with Functional Programming...

func reduceToZeroFP(x:Int) -> Int {
    x == 0 ? x : reduceToZeroFP(x: x - 1)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGabrielView Question on Stackoverflow
Solution 1 - SwiftesreliView Answer on Stackoverflow
Solution 2 - SwiftGeRyChView Answer on Stackoverflow
Solution 3 - SwiftLMLView Answer on Stackoverflow
Solution 4 - SwiftjoshdView Answer on Stackoverflow
Solution 5 - SwiftDK_View Answer on Stackoverflow
Solution 6 - SwiftdheeruView Answer on Stackoverflow
Solution 7 - SwiftJules BurtView Answer on Stackoverflow