0

I was creating a function and I tried to increase the value of the parameter which was declared as Integer. But it said that the value of that parameter can not be reassigned. I am a beginner so if I am missing some concept please tell me.

Plaban
  • 97
  • 1
  • 7

1 Answers1

1

Yes, in Kotlin function parameters can't be changed.

(However, if they refer to mutable objects, then those objects can change.  For example, if you declared fun a(b: List<String>), then you could add another String to the List; but you couldn't set b to refer to a different List.)

This is different from Java, where parameters are variable (unless specified as final).

This behaviour was announced in Kotlin milestone M5.1, where it was explained as avoiding confusion (especially in constructors), and promoting good style.

See also this answer.

gidds
  • 16,558
  • 2
  • 19
  • 26