Variables and constants
programSequences of instructions for a computer. usually use dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions. in some shape or form. Data in programs is usually referred to as values.
Variables
A variableA memory location within a computer program where values are stored. is a named memory address that holds a value. The value held in a variable can (and usually does) change as the program is running.
A variable's name is known as an identifier. 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.
- It should contain lowercase letters. However, uppercase letters can be used if a variable name comprises more than one word joined together.
- The name should be meaningful - it should represent the value it is holding.
| Acceptable identifiers | Unacceptable identifiers |
| score | Score |
| highScore | high score |
| high_score | 123score |
| highScore123 | $core |
| Acceptable identifiers | score |
|---|---|
| Unacceptable identifiers | Score |
| Acceptable identifiers | highScore |
|---|---|
| Unacceptable identifiers | high score |
| Acceptable identifiers | high_score |
|---|---|
| Unacceptable identifiers | 123score |
| Acceptable identifiers | highScore123 |
|---|---|
| Unacceptable identifiers | $core |
Variables make it easy for a programmer to use memory locations. The computer keeps track of which memory location the variable refers to. All the programmer has to do is remember the identifier the variable was given.
Declaration and assignment
Programming languages require a variable to be identified before a value is assigned to it. This is known as declaring a variable:
score as integer - this would declare a variable called score which will hold integers.
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 - this would assign the value 0 to the variable score.
Some programming languages, such as Python, enable variables to be declared and assigned a value in the same line of code.
Constants
A constant allows a value to be assigned a name. Unlike a variable, the value assigned to a constant cannot be changed whilst the programming in 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 - const PI = 3.142
Constants follow the same naming conventions as variables, except that they are usually in uppercase.
Global and local variables
A global variable is one that can be accessed and changed throughout the program. Usually a programmer has to declare that the variable is a global. For example:
global ID = 75
Local variables are confined to a loopA method used in programming to repeat a set of instructions. or subprogram. This has the advantage of the programmer being able to use the same variable names again and again for different purposes.
When using global variables, a programmer must be careful not to use a local variable with the same name. If this happens, the value of the global variable will be changed, causing the program to produce unexpected results.