[Term of the Day]: Global Variables

[Term of the Day]: Global Variables

Term of the Day

 

"Global Variables"

 

Definition — What are Global Variables?




Global variables are also called a global state or global environment, is a programming language construct, a variable type that is declared outside any function and is accessible to all functions throughout the program. This means that they can be changed by any function at any point and may affect the program as a whole.

In layman's terms, if variables were humans, Global variables have passports with the visa to go anywhere in the world if they are called. Like they can be available anywhere in the world.

Whereas, the Local variables don't have a passport and have to stay inside the country. They cannot be called abroad. However, they can travel inside the country any number of times.


Find some basic examples below to understand how a global variable can be used.

1) Create a global variable outside of a function, and use it inside the function.




2) If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.






3) The "global" Keyword

Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.





4) You can also use the global keyword if you want to change a global variable inside a function. To change the value of a global variable inside a function, refer to the variable by using the global keyword.










                  New to ADSelfService Plus?