Variables versus Constants

Declaring a constant is another way to work with data within a program, however, a constant’s value will never change during the running of a program.

A program written using only constants would be of little use because the same inputs would always result in the same outputs.

Like variables, constants have data types, including integer, real, character, boolean and string.

In order to make any computer program useful, variables must be used rather than just constants. Having said this, variables are not a replacement for the use of constants.

The advantages of using constants are:

  • Descriptive constant identifier names make code more readable which assists with program maintenance.
  • The value assigned to the constant can be updated as required without the need to change all of the instances of the value if a constant was not used.
  • Program testing is facilitated because constants can’t be accidentally changed without an error occurring.

It is important for a software engineer to understand the difference between variables and constants and when to use each.

Next: Naming Variables and Constants