1.3 Variables & Constants

Our programs need to have ways of storing and retrieving information. To store values that will never change we can use constants. For values that can change as the program runs we use variables. Either way we will need some way to refer to these values. We do this by using identifiers to name our values. We may choose any names we like for our identifiers provided that they follow the following rules:
  1. The first character must be a letter of the alphabet. Turing is case sensitive. This means that the identifier age is a different identifier than Age.
  2. If there are any other characters, they must be letters, digits or the underscore (_) character.
  3. An identifier must not be one of the following reserved words:

    Keywords

    allandarrayassert
    beginbindbodyboolean
    bycaseclosecollection
    constdecreasingdivelse
    elsifendenumexit
    exportexternalfalsefcn
    forforwardfreefunction
    getifimportin
    includeinitintinvariant
    labelloopmodmodule
    newnilnotof
    opaqueopenorpervasive
    pointerpostpreproc
    procedureputreadreal
    recordresultreturnseek
    setskipstringtag
    tellthentotrue
    typeunionvarwhen
    write

    Predefined
    Identifiers

    Just the more
    common ones

    see appedix
    for full list

    absarctanchrclose
    clscolorcolourcos
    datedelayeoffloor
    indexlengthlnlocate
    lowermaxminnil
    openordplayrand
    randintrandomizerepeatround
    screensignsinsound
    sqrtsystemtimeupper

Which of the folowing are invalid identifiers?

putputz
end2four6
great!twoWords
PUT

Note that putz is valid even though it contains a reserved word. Also PUT is fine since Turing distinguishes between upper and lower case letters. Reserved words are always all lower case.

Here is an example of declaring variables and constants. The variable isStudent will be of type boolean. The variables sum and entry will be reals. The variable count will be an int. The constant MAXIMUM will store the value 25 (note: I can't use the name max because it is a predefined identifier). A convention that we will use is to start identifiers for variables with a lower case letter. If the identifier has more than one word we will start the second and subsequent words with capitals (eg. isStudent). Constant identifiers will be all capitals. Although Turing will not force us to follow these conventions it will make our programs easier to read and understand. (And you won't lose marks needlessly on assignments!)

var isStudent : boolean
var sum, entry : real
var count : int
const MAXIMUM := 25 

You declare variables by putting the word var before an identifier name. This is followed by a colon and a type for the identifier. If you need to declare more than one variable of the same type you can include several identifiers separated by commas after the word var. For constants you put the word const in front of the identifier. This is followed by a colon equals and the value for the constant. You do not specify the type as Turing can figure that out based on the type of the value that you set the constant to.


Exercise 1.3

  1. Identify, with reasons, any illegal identifiers.
    1. Turing
    2. next_character
    3. const
    4. int
    5. max2
    1. 3rdPlace
    2. initalLetter
    3. case
    4. home-form
    5. a123456
  2. A program is to be written that records the results of a coin-tossing experiment. The program will need variables to record the number of heads, the number of tails, the total number of throws, the percentage of heads (correct to 4 decimal places), and the experimenter's name. Write a declaration to reserve spaces of the appropriate types for these variables. Be sure to use identifier names that show clearly the purpose of each variable.
  3. State three advantages of named constants.
  4. For each legal constant definition, state the type of the constant. If the definition is not legal, give a reason.
    1. YEAR := 1990
    2. const FIRST_ROW = "A"
    3. const TAX_RATE := .25
    4. const initialSpeed := 6.317e2
    5. const QUOTE := "\""
    1. const SUPERVISOR := "Ziggy T."
    2. const BIG := maxint
    3. const bigNeg = -maxint
    4. const last-row := "X"
    5. const HOME_PHONE = "555-1212"
  5. The following program has an error. Copy the program and run it on your system to see what type of error message you get.

    % This program is incorrect because it tries to change the value of a constant.
    const test := 0
    put test
    test := 1
    put test