Programming fundamentals - OCRVariables and constants

Programs are designed using common building blocks, known as programming constructs. These programming constructs form the basis for all programs.

Part ofComputer ScienceComputational thinking, algorithms and programming

Variables and constants

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

Variables

A 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 identifiersUnacceptable identifiers
scoreScore
highScorehigh score
high_score123score
highScore123$core
Acceptable identifiersscore
Unacceptable identifiersScore
Acceptable identifiershighScore
Unacceptable identifiershigh score
Acceptable identifiershigh_score
Unacceptable identifiers123score
Acceptable identifiershighScore123
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 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.