±«Óătv

Variables and constants

usually use in some shape or form. Data in programs is usually referred to as ‘values’.

Variables

A is a named location in that holds a value. For more information on memory, see the computers study guide. The value held in a variable can - and usually does - change as the program is running.

A variable's name is known as an . The identifier given to a variable usually follows certain rules:

  • It can contain letters and numbers but must start with a letter.
  • It must contain at least one letter (at the start of the name).
  • It must not contain special characters such as !@ÂŁ$%&* or punctuation characters. However, an underscore can be used. Spaces are not allowed.
  • The name should be meaningful - it should represent the value it is holding.
Acceptable variable identifiersUnacceptable variable identifiers
highScorehigh score (cannot contain a space)
high_score123score (must start with a letter)
highScore123$core (must start with a letter)
Acceptable variable identifiershighScore
Unacceptable variable identifiershigh score (cannot contain a space)
Acceptable variable identifiershigh_score
Unacceptable variable identifiers123score (must start with a letter)
Acceptable variable identifiershighScore123
Unacceptable variable identifiers$core (must start with a letter)

Variables make it easy for a program to store values in memory locations when it is running. The computer keeps track of which memory location the variable refers to. The programmer simply has to remember the name of the identifier the variable was given.

Declaration and assignment

Some require a variable to be declared before a value is assigned to it. How this is done varies by programming language. A variable does not need to be declared in pseudo-code before using it. However, when using real programming language it is good practice to always declare a variable before it is used. This ensures that a memory location is ready to hold a value.

For example, in Visual Basic.NET the instruction:

Dim score as integer

would declare a variable called score which will hold values.

Giving a variable a value is known as assignment. A variable must be assigned a value before it can be used. For example:

score = 0

would assign the value 0 to the variable score.

Some programming languages enable variables to be declared and assigned a value in the same line of code. For example:

Dim score as integer = 0

Constants

A enables a value to be assigned a name. Unlike a variable, the value assigned to a constant cannot be changed while the program is running.

Constants are useful because they are declared and assigned once, but can be referred to over and over again throughout the program. They are used for values that are unlikely to change, for example:

  • Pi, eg const PI = 3.142
  • VAT, eg const VAT = 20

Constants follow the same naming conventions as variables, except that they are usually in uppercase. This helps to make the programmer aware that they are accessing a constant rather than a variable.

Global and local variables

The area of a program where a variable is accessible is referred to as its scope. A global variable can be accessed and changed throughout the whole program. It is declared outside of a .

Local variables are usually confined to a subprogram. A variable with local scope will only be available within the subprogram and it is declared within the subprogram. Therefore, the programmer is able to use the same variable names again and again for different purposes. This makes easier as programmers know what section of code has access to the variables.

When using global variables, programmers must be careful not to use a local variable with the same name. Depending on the programming language, this could result in an error or the programmer updating the wrong version (local or global) of the variable. In general, global variables should be avoided as much as possible. This is because the value of the variable could be changed anywhere within the program and this makes debugging very difficult.

works slightly differently and a global variable has to be declared as such within a subprogram before it can be used.

For example, in Python you would use ‘global ID = 75’ within a function to access the global variable ‘ID’.