Global local variables and their scope

Global local variables and their scope

Variables have an intense role in any programming language. Languages like JAVA, C++, PHP, etc. implement OOP principles. These languages, therefore, have a wider scope for variables. However, that does not mean that global local variables are limited within these languages.

Global variables are variables that are declared outside any block, loop or function. These variables are especially important if you want to keep one variable and alter its value accordingly at different blocks of code. So, this changes the variable’s value whenever it is used.

Local variables on the other hand are limited to a particular block of code. These variables are used within a block for temporary calculations or value storage, and then they are destroyed. So, when we write a for-loop, we often initialize a local variable within the for-loop syntax.

Global local variables with an example

When a variable should be collecting values according the functionalities of different class functions, the variable needs to be a global function.

So, that means suppose we have a variable named ‘salary’. This variable is due to collect the salary value from separate code blocks – some from the main method, some from the grossPay method and some from the incentives method. So, instead of taking three different variables and maintaining their values, adding them; we can just update this global variable and yet our program structure follows a smooth line. Here’s an example:

int salary=0;

void main(){
    // Other statements here
    salary += 20000;
}

void grossPay(){
    // Other statements here
    salary += 1500;
}

void incentives(){
    // Other statements here
    salary += 1000;
}

Good implementation of global local variables is also pictured in case of local variables. Local variables have a unique role of their own. They especially come in use when programmers need quick declaration and usage. Just imagine, you need a for-loop or a while-loop and you need a counter for that. Is it feasible for you to move up to the starting portion of the code block or maybe even outside the loop block and declare variables with initialization? Moreover, it is impossible to maintain such huge number of variables and their names. Since all variables will be outside the blocks, they must have different names. Variable overloading would not work. Here’s an example:

void main(){
    // Other statements here
    salary += 20000; 
    // this salary variable would not be identified in the other two blocks
}

void grossPay(){
    // Other statements here
    salary += 1500; 
    // this salary variable would not be identified in the other two blocks
}

void incentives(){
    // Other statements here
    salary += 1000; 
    // this salary variable would not be identified in the other two blocks
}

Global local variables and scope

Global local variables have one important aspect to be kept in mind. When a programmer codes a huge project, scope can be one factor that would determine how easy or tough it would be for the former to get through the project. Scope enables quick declaration and usage thus enabling token or keyword overloading. Overloading is one of the prime aspects of OOP. Scope is nothing but the range till where a variable has its validity. Beyond this range a variable loses its current value and also is treated as a new variable.

Global variables have global scope. So, they can be used throughout the body of the program and you will never get a “not-found” or “not-declared” kind of error for these variables even in the inner most loops.

Local variables have scope within the block that they are declared. So, when a function body declares a variable or a loop declares a variable; they can have values retained until that block. In fact, outside this loop if you try to access it you would get an error message saying that variable is undefined.

Using scope of variables you could reap the maximum benefits of OOP. So, try it for yourself and let me know what you learned; here in the comments section.

Comments are closed.