It says the value of a variable can be changed part way through the code. You are using the example “my_number” in this case. How does the computer not get confused over which value belongs to the variable name as it is the same for both the old and the new number?
The latest assignment takes the place of the earlier one. The value first referenced is no longer in effect. my_number will now refer to new_number, as it were.
It’s very common to have variables change their values. In fact that is why we refer to them as variable. They are subject to change. The only ones that cannot be changed are constants.
Programs are meant to be dynamic, not static. We want to build in as much flexibility as possible so the code may be re-used.
function times_2_plus_3() {
return 5 * 2 + 3;
}
The above is a static function. It contains no variables, only literals and returns a constant value, 13. How can we make it dynamic and reusable? By letting the user input any value, not just 5.
function times_2_plus_3(number) {
return number * 2 + 3;
}
Now can reuse this function on any number, which will be doubled and added to 3.
Cleaning up the code only means removing redundancy, repetition and simplifying bloated code patterns. It does not mean manually reseting values on variables. The program needs to be able to do that on the fly in the normal course of action.
Perhaps you could post the code sample in question? That will give us something to bounce this discussion off of.
In the section “We could also change the value of my_number part way through our program:”, I can see how the values of my_number can be changed with the multiply * and division / symbols. Yet I see an equal sign = next to 1, & an equal sign next to 3. How can an equal sign change the value of a number since an equal sign means equivalent, after all equal sign in an equation can’t change the value of a number.