teach-ict.com logo

THE education site for computer science and ICT

2. Variables

A variable is to computing as a brick is to a building. It is absolutely basic to computing.

A variable is a named value or symbol that can be changed as the program runs.

For instance define a 'variable' called A and set it to a starting value such as 10 i.e. A = 10

Now anywhere in the program that needs to change that value can refer to A

For example

Let A = 10

then another part of the program changes it by doing something to it, like this

A = A + 1 which adds 1 to the variable called A

or

A = 3 which re-defines the value of A

or

A = B + 1 which copies the value of another variable called B and adds 1 to it.

Declaring a variable

Many computer languages insist that you 'declare' a variable before you can use it. This means you define the variable in terms of its name and data type, like this

int my_variable

In this case, a variable going by the name my_variable is to be used and it is an integer data type. We will cover integer a bit later.

Note the use of underscore, this is one way to avoid using spaces. Spaces make debugging software much harder than it needs to be. In fact many languages do not allow you to use spaces in a variable name.

Variables can be almost any name but it is a good idea to use a sensible description such as Sales_Price and not be too long. A common trick to avoid spaces and yet create a readable variable name is to use capitals within the name, like this

MyVeryLongVariableName

The capitals make it easier to see the words within the variable name.

Declaring a variable is usually a good idea for two reasons.

a) allows the compiler to reserve memory for it

b) allows the error diagnostics (de-bugging tool) to highlight data type errors or mis-typing it at a later part in the software.

However, not every language insist on it, some just let you use a variable as you go along, like this

my_variable = 1.223

or

new_var = old_var * 3 + 1.23

In this case the data type of the variable is implied in its value, such as string, integer, boolean, floating point and so on. (We cover these data types later)

Initialise a variable

The word 'initialise' simply means assigning a starting value to a variable, like this

my_variable = 1.223

or

my_name = "Henry"

or

my_logic_var = TRUE

Initialising a variable is usually a good idea because it helps define its type for the compiler (if you haven't declared it already) and also having undefined variables makes debugging more difficult.

Operating on a variable

The examples above shows that you manipulate variables with the use of operators. An operator manipulates the current value in some way to create a new value. With numeric variables you can use the usual mathematical operators +, -, /, * , mod.

With Boolean variables you can use logic operators such as NOT, AND, OR, EXOR.

Memory

A variable is actually a location in memory within the computer. The program changes the data held in that location by refering to the variable name. A single variable may use many bytes, it just depends on its data type and current value. For instance an integer data type will use two bytes whilst a string variable could use a large number of bytes depending on its content.

So declaring a data type is important, as at that point you are deciding to some extent on how much memory to use.

 

 

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: Declaring variable data typing