Variables Examined

A variable is an area of memory that has a unique name and value contained within it. This value can change as a program runs. If a variable is empty it is said to be undefined.

A variable is associated with a particular data type, such as integer or real.

Integer variables are used to store whole numbers such as 1 or 235 or 26749 and are normally used for counted data, such as number of items in stock or how many people took part in a survey.

Real (or floating point) variables are used to store numbers which have a fractional component, for example, a real variable called Salary could successfully take the values: 12000.98, 32456.78 and 105000.37. Real variables are normally used for measured data such as lengths, voltages, money etc.

In many programming languages only the values associated with the variable’s data type can be stored in the variable’s memory location while the program is running. Therefore, you couldn’t use an integer variable to store a real number or a character variable to store a string of characters. And any attempt to do so within program code could lead to program errors.

In many programming languages, variables must be explicitly declared, along with their data type, before they can be used. Some languages are not quite as strict allowing programmers to use variables without declaring them and their data type. However, it is good programming practice to make sure that you declare any variables before you use them in your programs.

Next: Variables versus Constants