fun test(temp: Int) {
temp = 5
}
The compiler throws an error saying "val cannot be reassigned".
Are local variables read only in Kotlin?
Function parameters are always read-only (i.e. declared as val
);
If you want to change it, you will need to use a (new) local variable:
fun test(temp: Int) {
var myTemp = temp
myTemp = 5
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With