What is a Variable? Local and Global Variable with Example

What is a Variable? 
   Variable is a name assign to a storage area that the program can manipulate. A variable type determines the size and layout of the variable's memory.

   It also determines the range of values which need to be stored inside that memory and nature of operations that can be applied to that variable.

   Variable. In mathematics, a variable is a symbol or letter, such as "x" or "y," that represents a value. In algebraic equations, the value of one variable is often dependent on the value of another. The data type, if not defined explicitly, is determined based on the initial value given to the variable.

   A variable is any characteristics, number, or quantity that can be measured or counted. A variable may also be called a data item. Age, sex, business income and expenses, country of birth, capital expenditure, class grades, eye colour and vehicle type are examples of variables.

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 under execution. After that, it will be destroyed automatically.


Example:
public int add(){
int a =4;
int b=5;
return a+b;
}
Here, 'a' and 'b' are local variables.

Global Variable:
   Global variables are defined outside of a subroutine or function. The global variable will hold its value throughout the lifetime 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.

What is a Variable? Local and Global Variable with Example

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post