Local and Global Variable
Local and Global Variable
A variable is “variable” because its value varies. It can be changed at any time without issue.
A constant is, well, constant. Its value will never change. It remains the same throughout the program. You can try to
change the value of a constant indirectly through a pointer (foiling your compiler if it’s not too smart), but your
program will still crash.
Scope of Variables
The scope of the variable is simply lifetime of a variable. It is block of code under which a v ariable is applicable or
alive. For example:
function foo(){
var x;
}
You declare a variable "x" inside a function "foo." The scope of that variable remains inside that function it can't be used outside of t hat
function.
There are three places where variables you can declare variable programming language:
A local variable is a type of variable declared within programming block or subroutines. It can only be used only inside that subroutine or
code block in which they were declared. The local variable exists until the block of the function is in und er execution. After that, it will be
destroyed automatically.
Global variables are defined outside of a subroutine or function. The global variable will hold its value throughout the life time of a program.
They can be accessed within any function defined for the program.
Example:
int a =4;
int b=5;
public int add(){
return a+b;
}
Here, are some fundamental differences between Local and Global variables.
Lifetime It is created when the function starts It is created before the program's global
execution and lost when the functions execution starts and lost when the
terminate. program terminates.
Data sharing Data sharing is not possible as data of the Data sharing is possible as multiple
local variable can be accessed by only functions can access the same global
one function. variable.
Parameters Parameters passing is required for local Parameters passing is not necessary for a
variables to access the value in other global variable as it i s visible throughout
function the program
Modification of When the value of the local variable is When the value of the global variable is
variable value modified in one function, the changes are modified in one function changes are
not visible in another function. visible in the rest of the program.
Accessed by Local variables can be accessed with the You can access g lobal variables by any
help of statements, inside a function in statement in the program.
which they are declared.
Memory storage It is stored on the stack unless specified. It is stored on a fixed location decided by
the compiler.
You can access the global variable from all the functions or modules in a program
You only require to declare global variable single time outside the modules.
It is ideally used for storing "constants" as it helps you keep the consistency.
A Global variable is useful when multiple functions are accessing the same data.
The use of local variables offer a guarantee that the values of variables will remain intact while the task is running
If several tasks change a single var iable that is running simultaneously, then the result may be unpredictable. But declaring it as
local variable solves this issue as each task will create its own instance of the local variable.
You can give local variables the same name in different functi ons because they are only recognized by the function they are
declared in.
Local variables are deleted as soon as any function is over and release the memory space which it occupies.
Actual arguments
Arguments which are mentioned in the function call is known as the actual argument. For example:
1 func1(12, 23);
here 12 and 23 are actual arguments.
Actual arguments can be constant, variables, expressions etc.
1 #include<stdio.h>
2 void func_1(int);
3
4 int main()
5 {
6 int x = 10;
7
8 printf("Before function call\n");
9 printf("x = %d\n", x);
10
11 func_1(x);
12
13 printf("After function call\n");
14 printf("x = %d\n", x);
15
16 // signal to operating system program ran fine
17 return 0;
18 }
19
20 void func_1(int a)
21 {
22 a += 1;
23 a++;
24 printf("\na = %d\n\n", a);
25 }
Here the value of variable x is 10 before the function func_1() is called, after func_1() is called, the
value of x inside main() is still 10. The changes made inside the function func_1() doesn’t affect the
value of x . This happens because when we pass values to the functions, a copy of the value is made and
that copy is passed to the formal arguments. Hence Formal arguments work on a copy of the original
value, not the original value itself, that’s why changes made inside func_1() is not reflected
inside main().