Variables and Constants

Variables

A variable is something that may change in value. A variable might be the number of words on different pages of this booklet, the air temperature each day, or the exam marks given to a class of school children.

A variable could be likened to a storage box whose contents may often change. The box, or variable must be given a name to distinguish it from others. According to FORTRAN rules, the variable name must begin with a letter and may be followed by up to five characters (letters or numbers only).

Variables in FORTRAN are of different types. You specify the type and name of each variable you intend to use at the top of your program, after the PROGRAM statement and before any other executable lines. Commented lines are non executable so they can appear anywhere in the program.

If the variable declarations are omitted, the compiler will make certain assumptions, for example, that any variables beginning with the letters I, J, K, L, M, N are INTEGER. The lack of specification often leads to program errors and it is strongly recommended that variable types are always declared. Numerical data may be separated into integer and real numbers.

Integers

Integers are whole numbers, those without a decimal point, (for example 7, 4563, 99) and are stored in integer variables.

The general form of the declaration of integer variables is:


	  INTEGER namel, name2
	  

Example

  • INTEGER WHOLE, AVERAGE, SUM
  • Reals

    Reals are fractional numbers, those including a decimal point, (for example, 0.2, 653.46, 1.0) and are stored in real variables. Real numbers cannot be stored accurately. The accuracy of a real variable depends on the computer.

    The general form of the declaration of a real variable is:

    	  
    	  REAL namel, name2
    	  
    

    Example

  • REAL FRACT, MEAN, STDDEV
  • Characters

    Character variables contain one or more characters, (for example G or OXFORD).

    The general forms are:

    
    	CHARACTER namel,name2		where name1 and name2 are 1 character each.
    
    	CHARACTER*n namel,name2		where name1 and name2 are n characters in length each.
    
    	CHARACTER namel*nl,name2*n2	where name1  is of length n1 and name2 iS of length n2.
    
    

    Constants

    Constants are quantities whose values do not change during program execution. In FORTRAN they may be of numeric or character type.

    Double Precision

    Real data values are commonly called single precision data because each real constant is stored in a single memory location. This usually gives seven significant digits for each real value. In many calculations, particularly those involving iteration or long sequences of calculations, single precision is not adequate to express the precision required. To overcome this limitation , FORTRAN provides the double precision data type. Each double precision is stored in two memory locations, thus providing twice as many significant digits.

    The general form of the declaration of a double precision variable is:

    	  
    	  DOUBLE PRECISION namel, name2
    	  
    

    Example

  • DOUBLE PRECISION ROOT, VELO

  • Work your way through the following components attempting the exercises as you come across them:

    | Programs | Variables | Arithmetic Operations | Input and Output | Looping in Programs | Arrays in Programs | Checking variables | Subprograms and functions |