What is the difference between global and local variables




















The local variable exists until the function executes. After completing the execution of that function, the control is passed back to the main method. Therefore, the local variable only exists until the function executes. After finishing the execution of the function, the local variable is destroyed.

A new variable called area is created inside that function. This variable is a local variable. It is accessible only within that function. It cannot be accessed inside the main method. A global variable is a variable that is declared outside all functions inside the program.

There can be multiple functions in a single program. All these functions can access these global variables. Therefore, a global variable is not very secure as the value can be changed by other functions. A global variable exists until completing the execution of the whole program. Global variables are helpful when multiple functions operate on the same data. On the other hand, the value of a global variable is not reliable as it can be changed.

The sum function prints the summation of a and b. Therefore, the global variables are accessible by all the functions in the program. In any programming, language variables are classified into local and global variables for declaring any variable that will store some value along with space in the memory for the program to execute.

In general, local variables are defined as variables that are and can be declared within any function or any statement block, and such variables are called local variables, whereas Global variables are defined as variables that are and can be declared outside any function or any statement block but within the program in the most cases at the beginning of the program these variables are declared and such variables are called as Global variables.

The scope of a global variable is defined and declared in such a way that these global variables are available or the scope of these variables remains throughout the program which means to ay functions or any statement blocks and accessibility of these global variables is such that these variables can be accessed anywhere from the program or any function or any statement block these variables can be accessed but within the program not outside the program.

Whereas Local variables are defined and declared in such a way where these local variables are made available or the scope of these local variables remains only within that function or any statement block in which these local variables are defined and the accessibility of these variables is such that these variables can be accessed only within those functions or statement blocks in which these variables are defined but not outside or throughout the program.

Initializations of the global variables are initialized by the system and are not initialized by any developers or ourselves and the initializations of global variables are not initialized with any values by the system then it will store zero as value.

Whereas local variable initialization is not done by the system instead the developers or we can initialize these variables and if the initialization is not done by developers then it stored the garbage value and not zero as in global variables. Global variables are used where the variables are required to be accessed throughout the program which includes all functions or any statement blocks within the program by only declaring once the same data which is declared at the beginning can be used.

Whereas, local variables are used only when any particular function or any statement block requires a variable declaration and these are only defined within that function and are also used only with that function. In this article we conclude that the global and local variables are both important in their perspective it depends on the program the developer is writing to chose between these variables and sometimes both can be used within one program.

This is a guide to Local Variables vs Global Variable. Here we also discuss the Local Variables vs Global Variable key differences with infographics and a comparison table. You may also have a look at the following articles to learn more —. Submit Next Question. Hey, I am Global Variable! Hey, I am Local Variable! Value of non-static variable is: 20 Value of static variable is Next Topic Difference between. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow.

Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering.

Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. What is a Variable?



0コメント

  • 1000 / 1000