Global variables can be accessed from anywhere in a computer program; local variables can only be seen in a particular part of a program. In general, global variables are used for constants. Local variables are used for program flow.Continue Reading
Global variables can be accessed from anywhere in a program, so variables that do not change are generally global in scope. A program that relies on a value of pi, for example, might use a global variable. They are often placed at the beginning of a program where they are easy to access, and constant global variables are often spelled with all capital letters.
Local variables, on the other hand, are typically used in a single function or method. This allows programmers to reuse variable names. The variable "i" is often used for iteration, and making it local allows it to be used whenever necessary.
Experts strongly recommend using global variables only when necessary. Local variables can be removed from memory when they are not needed, and using local variables whenever possible can prevent a number of potential bugs. Data segregation helps make programs more understandable, and the ability of objects to keep data accessible in as few places as possible is part of the reason why object-oriented programming is so popular for complex programs.Learn more about Computer Programming