Global vs. Local Question


I need a little help understanding what's going on in Functions and Variables: 10. Global vs. Local Variables.
I understand what you have to do, but I don't understand how it changes it. I see the change, but how does changing the variable inside the function change the outcome outside of the function, while inside the function, it stays the same? I don't want to move on without understanding the logic behind it, if anyone could help me out.


It is sort of like math, meaning, with an example
Local: 2+(2+2) so in this the addition in the brackets is local and affects the outcome indirectly.
Global: this is straightforward addition 2+2+2.

Hope this helped :grin:


The point of this exercise is that by adding a var to the variable name inside the function you create a second variable with the same name that is independent from the first one.

Without the var my_number is a global variable so inside and outside of the function you only have one variable and therefore the inside effects the outside. Now as you have 2 of them they are independent and therefore changing the inner one has no effect on the outer one. This is called shadowing because now the local variable shadows the global one, meaning you can no longer reach the global one from the function although it is global.