大象传媒

Variables and constants

usually use in some shape or form. Data is often stored within a program using variables and constants.

Variables

A is a named piece of memory 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 known as a naming convention:

  • 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
highScorehigh score
high_score123score
highScore123$core
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

Most require a variable to be identified before a value is assigned to it. This is known as declaring a variable. In Visual Basic, this would look like:

Dim score as Integer - would declare a variable called score which will hold integers.

Giving a variable a value is known as assignment. For example, giving the variable above a value would look like the following in Visual Basic:

score = 0 - would assign the value 0 to the variable score

Some programming languages, such as Python, do not require variables to be explicitly declared before use.

Constants

A constant is a named piece of memory where the value cannot be changed while a program runs.

Constants are useful because they are declared and assigned once, but can be referred to over and over again throughout the program. This means that if the programmer needs to change the value throughout the program code, they only need to make one change. This can help make a program easier to maintain.

Constants follow the same naming conventions as variables, except that they are often written in uppercase. Some programming languages, such as Python, do not support constants.

Constants are used for values that are unlikely to change, for example:

Pi - PI 鈫 3.142

Global and local variables

A global variable is one that can be accessed and changed throughout the whole program. Local variables only exist within a particular subroutine.

Using local variables rather than global variables makes it easier to debug a program as the value of that variable can only be read or changed within one subroutine. Another advantage of local variables is memory efficiency. Once a subroutine has finished running, the memory used for all local variables is removed.

In general, the use of global variables should be avoided wherever possible.