Global vs Local Variables
Now that we've learned about functions it's time to understand an important topic, the difference between global and local variables. When we create a variable within a function is it said to be local to that function. This means that we cannot access variables that we've created within that function outside of it.
When we create variables in the main-line (not within a function) they are said to be global. This means that all of our functions can access these variables so long as they are defined after the variable is created.
Many times we would like to change a global variable from within a function. However, have a look at what happens when we try this below.
When we try to change the value of x from within the function what we actually end up doing is creating a new variable local to myFunc. To avoid this problem we can use the "global" keyword.
It is typically not good practice to use global variables. This is because we usually design functions that are meant to be reused in future programs. So avoid using global variables if possible.
One case where a global variable is acceptable is when creating project/program specific functions that will not be re-used.