Variables play an important role in the world of programming. Every programming language comes with the concept of variables. These variables act as a name for the memory location. A variable is a token in a programming language that is a user-given name. This name is meant to assign some values to a storage area that can be manipulated by the program. Variables can be either local or global. In this article, you will learn the difference between local and global variables.
Characteristics of Variables:
A variable along with its type determines the size, location of storage, and the layout based on which it will be represented in the memory. Apart from the type of variable, there is a concept of scope. Before understanding the local and global variables, it is necessary to comprehend how the scope works in Python.
Scope of a Variable:
We can define the scope of a variable as the lifetime of a variable. It is the block, suite, or section of code under which a variable stays alive, performs its work, and remains accessible as a storage name. In Python, there are essentially two main types of variables. These are global and local variables. The scope in which these variables get declare defines their type. Let's talk about global and local variables.
Global variables in Python:
Global variables are those Python variables that do not reside within a function's scope or within any other block. Thus, you can say that they have a Global Scope. It means, these variables can be accessed from anywhere within the entire program, including within functions and nested functions. Making a variable Global allows the programmer to use it throughout the code without any restriction. If there is no variable defined inside the function scope, the global variable is used inside the function.
def runn(): # no variable with the name strr defined within the local scope print(strr) # strr created within the Global scope strr = "I'm in love with Python" runn()
I'm in love with Python
First, we will define a user-defined function runn() and print the strr variable within that function body. But the question is where this variable comes from. This variable is not a local variable as it has not been declared within the function (hence not local). In the global scope, a variable strr is declared and initialized with a string. Finally, we call the runn() function. It automatically uses the global variable as no other variable with that name is defined.
Another way to make a variable global at any point within the program is by using the global “keyword”.
def setA(val) : global var # declaring var declared within a function but meant to be a global # because of the keyword explicitly attached to it var = val # assigning the global value of var def runn() : print(" The new Value is ", var+2) setA(10) runn()
The new Value is 12
Here we create a user-defined function seta() and passed a parameter “val”. Inside it we create a variable var and explicitly declare it as a global variable using the global keyword. Then we assigned the value of val to var. We then create another function runn() and used the var from another function. Since it’s a global variable, it will work from that other function also. Finally, outside the function, i.e., within the global scope, we called the function.
Local Variables in Python:
When a variable is defined within a function's body or a local scope or block, we call such variables a local variable. These variables are known to have a local scope. Local scope means that these variables can be used or accessed within the function's scope or the suite's scope in which it is declared. You can never invoke it or use it from another function or block. If a programmer tries to access such variables from another function or block, a compile-time error will pop up.
def runn() : # the variable val is defined within the scope of the function, i.e. function body val = "This is a local variable" print(val) runn()
This is a local variable
First, we will define a user-defined function runn() and initialize the local variable (val) with a string value. Then we print that variable (val) using the print(). From the global scope of the program, we called the runn() function.
Difference between Local Variables and Global Variables:
|Local Variables||Global Variables|
|We have to declare local variables within a function, i.e., in the function body.||We should declare global variables outside a function.|
|Local variables are usually stored in stack section of the memory.||Global variables are usually stored in the private heap section of the memory.|
|It gets created with the function starts executing and lost when the functions ends.||It gets created when the program’s global execution section starts and lost as the program ends.|
|You cannot share the data residing in a local variable outside the function because it gets lost once the execution flow exits the function body.||You can share the data residing in a global variable outside the function and anywhere within the program because it remains until the program terminates.|
|When programmers make changes in the value of a local variable within a function, it does not impact other functions having the same variable name residing in another function.||When programmers make changes in the value of a global variable, it does have an impact on all the functions and operations using that variable.|
|The parameters programmers’ uses inside a function are local variables.||Global variables do not have a direct connection with function parameters.|
|If a local variable is suspended or removed, it does not hamper other functions having the same variable name, because each local variable is independent in its own function.||If global variables are suspended from using it within the code, the programmer will have to remove all the variables from those functions. Alternatively, the programmers might need to eliminate the functions where they are called.|
Apart from all these, if a variable is declared in the local scope and that same variable name is declared in the global scope, the variable in the local scope gets the priority over the global scope when used within a function.
def funcc(): # redefined in the local scope strg = "Same Here inside the Function scope" print(strg) # Global scope strg = "I am the Original One" funcc() print(strg)
Same Here inside the Function scope I am the Original One
First, we will define a user-defined function funcc() and initialize the local variable (strg) with a string value. Then we print that variable using the print() function. We then create another variable in the global scope but with the same name strg and initialized it with a different string this time. Now when we tried calling the function funcc(), the local variable got more priority over the global inside the function. Whereas, when the print() function invoked the strg, the global variable got more priority.
Advantages of Local variables:
- Global variable assures that the value it holds will remain intact while the function or the set of statements performs its task.
- As soon as the function terminates, the local variable gets deleted. This frees up the memory space that it occupied.
- Creating a local variable within a function will create its own instance when a task will be performed. This reduces the problem of getting different values or unpredictable initialization of values in variables.
- Local variables have local scope, and hence they work in a particular area, making the program less complex.
- Local variables in different functions can have the same name. It is because they get recognized only by the function in which they are declared.
Advantages of Global variables:
- Programmers can access global variables from any function or module, making it flexible to work with.
- Programmers need to declare a global variable only a single time outside any function or block scope.
- Global variables are used in situations where the programmer wants to store values that are required in multiple functions with some updates in them.
- Programmers can store fixed or constant values in global variables. This will reduce the complication of the program and helps in preparing the program with more readability.
- Global variables help in the easy debugging of a program.
- Global variables make it easy and efficient (in terms of space complexity) to store variables where multiple functions are accessing the same data.
Both local and global variables play a significant role in programming. They have their own characteristics and uses. It is very important to understand the scope and lifetime of a variable and where to declare which variable. Because, depending on the declaration, you program will meet bugs and errors. So, if you want to use a variable that will be used at multiple location of the same program, declare the variable as global, otherwise go with the local variable creation.