In Dart, it is allowed that change the value of parameter in method like this
void changeValue(int num) {
num = 10;
}
I know that above code isn't occur Error, but num = 10; can't change actual value of argument.
(Because Dart is 'pass by value')
my questions are these..
- Is it okay to change the value of parameter?
- Is there any other reason or intention for Dart allowing developer can change value of parameter?
It's OK to change the local variable introduced by a function parameter, if you can and want to.
You can make it impossible by making the variable final:
Most parameter variables could be final, but nobody bothers to write the
final, because a mutable variable that nobody assigns to is just as good. Especially if nobody thinks about assigning to them.Cases where I would assign to the variable include:
Your parameter variables are not final unless you want them to. Reusing them is perfectly fine, as long as you don't change the meaning of the variable. It's usually fine if the original value can reach all uses of the variable at least once. If not, then it may not be the same conceptual variable any more.