Variable Declaration and Initialization
Published: 2022-04-11 18:47:40


## VARIABLES

In Programming languages, variables allow programs to store, load, and manipulate information which can be referenced later on. They also provide a means to store data with a label, this label is usually a descriptive name to give a developer an idea of the type of data stored. 

### Variable properties
There are 4 major properties associated with variables;

- A name
- A type
- A value
- Scope

The name of a variable should describe the information that is stored within the variable. The name is very important because this is how we access the value stored in a location in memory.

The type refers to the kind of data being stored, in some programming languages the data type must be defined before initializing the variable. This tells the compiler the kind of value to expect. Languages like this are, C++, Java, etc.

In other languages, the data type of each variable doesn't need to be specified before initialization, for this case, the data type is inferred based on the value passed. Example is Python;

All actions carried out within variables can be grouped into three major parts;
- Variable Declaration
- Initializing the Variable: Access(Read) and Assign(Write)

### Variable Declaration
In declaring variables, this is basically where we announce the existence of the variable to the computer, the declarations simply gives an identifier to a value stored in memory
The code below is a variable declaration example in type script

`let age: number;`

Here the identifier age refers to a number value stored at a certain location in memory, the general syntax for declaring variables her is

`let <name> : <type>;`

for C++ the syntax for declaring variables is

`<type> <name>:`
example 

`int age; `

This tells the processor to reserve  a location in the computers memory, this location should be enough to store any value of integer type. 
After declaring the variable, whenever we refer to the variable name, the program knows the exact location in memory it reserved.

There are specific rules to how we name variables;
- Variables can not contain spaces
- Variables can only contain letters, numbers and undrescores.
- Variables names must begin with a letter



### Initializing a Variable
Initializing a variable refers to the process wherein a variable is assigned its' initial value before it is used within the program. 
Initializing a variable comes after declaring the variable. A variable can not be used without first declaring it. 
In some programming languages, the declaration and initialization process can be done in one line.

In python,  if we say `age = 50`, the age variable is first declared and we don't need to specify the data type, since we passed in an integer, python infers the data type automatically.

### Assigning a value
Considering the example below

```
height = 20
height = height + 60
```

First the program, reserves a location in memory and assigns the label 'height' to it, it then stores the value 20 in that memory location.

For the second line, python first evaluates what's on the right hand side, then passes that value to the memory location with the label 'height'. 
On the right hand side, python is faced with a variable `height`, it goes to the location in memory with this label and retrieves the value stored within that location. 
The value stored here is 20, it then retrieves the stored value and evaluates 20 + 60 to 80, this is the new value.
This value is then stored in a location in memory, then it's labelled 'height'. 

So whenever we call the variable height, the program searches for the location in memory with the label height, and returns the value within.