Difference Between Local and Global Variables
Local vs Global Variables
Global variables can be used anywhere in a computer program. This means that global variables can be used in many functions. This global variable can be used in various user defined header files as well as java packages. The global variables can also be changed programmatically.
When talking about local variables, it is just a local computer programming or is local to a function. Local variables cannot be used beyond the particular function. The lifetime or scope of a local variable is just within a procedure or a block whereas the scope of a global variable is throughout the program.
Local variables are just used in the function where they have been declared. Moreover, the local variable only remains lifetime as long as the sub or function is in vogue. Once the program comes to an end, the local variable gets erased from the memory.
While modifications in a global variable can be made from anywhere, it cannot be done with local variables. If the global variable is present in protected memory, modifications cannot be made in a global variable.
There are many advantages with local variables and global variables. An added advantage of the local variable is that it makes it easier to debug and maintain the applications. But in the case of global variables, one cannot be sure in which function it will be modified or when the variable values will be modified. On the other hand, in a local variable, there is nothing to trace. In the case of local variables, there is an advantage that it comes with fewer side effects when compared to global variables.
Summary:
1.Global variables can be used anywhere in a computer program. When talking about local variables, it is just a local computer programming or is local to a function.
2.This global variable can be used in various user defined header files as well as java packages. The global variables can also be changed programmatically.
3.The lifetime or scope of a local variable is just within a procedure or a block whereas the scope of a global variable is throughout the program.
4.While modifications in a global variable can be made from anywhere, it cannot be done with local variables.
5. An added advantage of the local variable is that it makes it easier to debug and maintain the applications. But in the case of global variables, one cannot be sure in which function it will be modified or when the variable values will be modified. On the other hand, in a local variable, there is nothing to trace.
- Difference Between CNBC and Fox Business - October 3, 2011
- Difference Between Distilled Water and Boiled Water - September 30, 2011
- Difference Between McDonalds and Burger King - September 30, 2011
Search DifferenceBetween.net :
Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.
thanks for explaining it, I was very confuse about that.
Thnx alot.. Its really helpful
great article, very useful for beginner like me 🙂
In C++
Global means you can use it in any event, local means you can only use it in the event where it’s declared.
Example, C++:
#include
using namespace std;
bool x; //Global Boolean variable, can be used anywhere
void test()
{
//x can be used here, but y can’t
}
int main();
{
int y; //Local y integer, can only be used in this event
{
VB6:
Dim x as string ‘global x variable, since it’s outside the events
Private Sub Command1_Click()
Dim y as integer ‘local y variable, since it’s inside an event
End Sub