0

I'm new to kotlin. I've got a problem with making a function that changes the value of the boolean.

fun main () {

    var x = false

    functionWithBoolean(variable = x)

}

fun functionWithBoolean(variable: Boolean) {

    if (variable){
        println("x is true, so I switch to false")
        variable = false //value of x should be changed from true to false
    }

    else if (variable == false){
        println("x is false, so I switch to true")
        variable = true //value of x should be changed from false to true
    }

}

The error I encountered: Kotlin: Val cannot be reassigned

Please for explanation how to make "variable" var, altough x is set for var.

Marcel
  • 15
  • 3
  • For usual functions the params are always fixed (`val`). If you have complex logic that need saving of a other state you need to create a new local variable at the function scope (inside the function). So for you the variable `variable` is fixed. Because `variable` param is call-by-value you can overwrite `x` using return value inside the function and assigning it (`val result = sum(2,2)`). Collections can be changed without returnvalue because they are call-by-reference. – LenglBoy Apr 09 '23 at 21:50

4 Answers4

1
var x = false

In this x can be reassigned

But when you pass the value as an argument to functionWithBoolean you are passing are reference and so even if you did change it, it won't alter the source.

However, if you pass back the value as a return via this simple change, you will achieve what you want:

    fun main () {
        var x = false

        x = functionWithBoolean(variable = x)
    }

    fun functionWithBoolean(variable: Boolean): Boolean {

        if (variable){
            println("x is true, so I switch to false")
            return false //value of x should be changed from true to false
        }

        else{ // you don't need:  if (variable == false)
            println("x is false, so I switch to true")
            return true //value of x should be changed from false to true
        } 

    }
AndrewL
  • 2,034
  • 18
  • 18
  • why did you add an additional Boolean after declaring an argument? (7th line) – Marcel Apr 09 '23 at 21:41
  • Because in my modification I am RETURNING a Boolean from the function, hence the two `return` statements within the function. Not that there is NO relationship between the `x` at the beginning and the end of this line `x = functionWithBoolean(variable = x)` all of the following are also perfectly ok: `x = functionWithBoolean(false); val y = functionWithBoolean(x); println(functionWithBoolean(x))` – AndrewL Apr 09 '23 at 23:11
1

To change a variable in a function, you would have to pass an actual variable setter, not just the value of the variable. So you would have to give your function a variable-setting parameter. This is just an example of how you could actually achieve this. In most cases, this would be considered convoluted design.

I changed "variable" to "input" because it doesn't make sense to call your function parameter a variable. You cannot directly pass variables around, only values.

fun main () {

    var x = false

    functionWithBoolean(input = x, useNewValue = { x = it })

}

fun functionWithBoolean(input: Boolean, useNewValue: (Boolean)->Unit) {

    if (input){
        println("x is true, so I switch to false")
        useNewValue(false)
    }

    else if (input== false){
        println("x is false, so I switch to true")
        useNewValue(true)
    }

}

This design is convoluted and error-prone, so usually you would simply return a new value from the function and assign it to your variable:

fun main () {

    var x = false

    x = functionWithBoolean(input = x)

}

fun functionWithBoolean(input: Boolean): Boolean {

    return if (input){
        false
    }

    else {
        true
    }

}

Or more simply:

fun functionWithBoolean(input: Boolean): Boolean {
    return !input
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

By passing the variable x into a function, you pass-by-value, not pass-by-reference you can imagine that a copy is passed. You might find a solution here (the same problem, posted Mar 1, 2017) or here, but normally you would not change the parameter value from a function.

source

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

I find other answers sort of correct with this, val cannot be reassigned directly and there are some valid solutions about that. I also found that things with var properties can have those properties changed and they stay changed outside of functions.

This includes Array, List, and DoubleArray. In a function array[0] may be changed and used but you can't assign array unless it is a var property of something, use a BooleanArray for simplicity.

fun main () {

    var x = BooleanArray(1){false};

    functionWithBoolean(x)

}

fun functionWithBoolean(variable: BooleanArray) {

    if (variable[0]){
        println("x is true, so I switch to false")
        variable[0] = false //value of x should be changed from true to false
    }

    else if (variable[0] == false){
        println("x is false, so I switch to true")
        variable[0] = true //value of x should be changed from false to true
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
maybeJosiah
  • 1
  • 1
  • 9