You are on page 1of 4

What is the difference between a constant and a 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:

 Inside a function or a block: Local variables


 Outside of all functions: Global variables
 In the definition of function parameters: Formal parameters

Definition of Local Variable

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.

Example of Local Variable

public int add(){


int a =4;
int b=5;
return a+b;
}

Here, 'a' and 'b' are local variables

Definition of Global Variable

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, 'a' and 'b' are global variables.


Local Variable Vs. Global Variables

Here, are some fundamental differences between Local and Global variables.

Parameter Local Global

Scope It is declared inside a function. It is declared outside the function.

Value If it is not initialized, a garbage value is If it is not initialized zero is stored as


stored default.

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.

Advantages of using Global variables

 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.

Advantages of using Local Variables

 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.

Disadvantages of using Global Variables


 Too many variables declared as global, then they remain in the memory till program execution is completed. This can cause of
Out of Memory issue.
 Data can be modified by any function. Any statement written in the program can change the value of the global variable. This may
give unpredictable results in multi-tasking environments.
 If global variables are discontinued due to code refactoring, you will need to change all the modules where they are called.

Disadvantages of using Local Variables

 The debugging process of a local variable is quite tricky.


 Common data required to pass repeatedly as data sharing is not possible between modules.
 They have a very limited scope.

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 func1(a, b); // here actual arguments are variable


2 func1(a + b, b + a); // here actual arguments are expression
Formal Arguments
Arguments which are mentioned in the definition of the function is called formal arguments. Formal
arguments are very similar to local variables inside the function. Just like local variables, formal
arguments are destroyed when the function ends.
1 int factorial(int n)
2{
3 // write logic here
4}
Here n is the formal argument.
Things to remember about actual and formal arguments.
1. Order, number, and type of the actual arguments in the function call must match with formal
arguments of the function.
2. If there is type mismatch between actual and formal arguments then the compiler will try to
convert the type of actual arguments to formal arguments if it is legal, Otherwise, a garbage value
will be passed to the formal argument.
3. Changes made in the formal argument do not affect the actual arguments.
The following program demonstrates this behaviour.

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().

You might also like