Answer the question
In order to leave comments, you need to log in
Why can't the method increment the variable passed to the arguments?
And I pass a mutable variable var number = 5 to the method
printNumbers(number: Int) {
number++ // подчеркивает красным, предлагая создать локальную переменную
}
Answer the question
In order to leave comments, you need to log in
These are the features of kotlin. Function arguments are immutable (we can conditionally assume that they are val).
I do not remember a single case when I would need to change the function argument.
So there is only one way out - always overwrite in a local variable?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question