Python: Global Variables?

techi3

In Runtime
Messages
119
I've been reading Think Python and I'm having trouble with understanding what a global variable is. Can anyone help me understand it or point me to a better Python study guide? Thank you.
 
in the context of programming..


a global variable is one that is accessible outside of a specific sub routing.



so

int number
in main()
{
...Main function...
}

int subfunction()
{
...sub function code...
}

in this example the variable called number is available to both the main function and the sub function. writting print number in either will show the contents of that variable.


int main ()
{
int letter
int number;
...more main code...
}
int subfunction()
{
int number
...more code...
}

here the variables are declared inside the functions, and thus local to the functions. the variable called letter only appears inside the main function and you can't use it in the subfunction.

the variable called number appears in both the main and the subfunction. but crucially it's a local variable in each case, whilst it has the same name, underneath the program it has a different memory reference and has/can have different data stored within it.
 
Back
Top Bottom