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 鈥榲alues鈥.
Variables
A variableA memory location within a computer program where values are stored. is a named memoryThe part of a computer that stores data. location in RAMRandom access memory. This is volatile memory that is constantly being written to and read from. It does not retain its contents without a constant supply of power. When a computer is turned off, everything stored in its RAM is lost. 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 identifierA name given to a part of a program, such as a variable, constant, function, procedure or module.. 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 identifiers | Unacceptable variable identifiers |
highScore | high score (cannot contain a space) |
high_score | 123score (must start with a letter) |
highScore123 | $core (must start with a letter) |
Acceptable variable identifiers | highScore |
---|---|
Unacceptable variable identifiers | high score (cannot contain a space) |
Acceptable variable identifiers | high_score |
---|---|
Unacceptable variable identifiers | 123score (must start with a letter) |
Acceptable variable identifiers | highScore123 |
---|---|
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 programming languageA language used by a programmer to write a piece of software. 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 integerA whole number - this is one data type used to define numbers in a computer program. Integers can be unsigned (represent positive numbers) or signed (represent negative or positive numbers). 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 constantA value in computer programming that does not change when the program is running. 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 subprogramA small program that is written within a main program..
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 debugThe process of removing errors from a program. 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.
PythonA high-level programming language. 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 鈥榞lobal ID = 75鈥 within a function to access the global variable 鈥業D鈥.