http://ponce.sdsu.edu/fortran_book_03.html





CHAPTER 3

 

DECLARATION STATEMENTS

 

A computer stores data (e.g., the values of variables) in units called bytes. A byte is a unit of storage consisting of 8 bits. A bit (short for binary digit) is a binary numeric representation, which is capable of storing only the digits 0 or 1. Computers can store data using either 2, 4, 8, or 16 bytes per value. For instance, a computer that uses 4 bytes (32 bits) to store a real variable is referred to as having a 4-byte wordsize or a 32-bit wordlength.

A variable defines a storage location in the computer's memory. Some processors assign initial values to variables, so that all variables are defined at the start of program execution. Alternatively, the DATA statement is used to explicitly provide initial values for variables. During execution, values can be changed at any time and as many times as needed, simply by assigning a new value to the variable. At any time, the value of a variable is the value currently stored in that variable's storage location.

A variable name identifies a variable within a program unit. A valid variable name must begin with a letter (A through Z), and is limited to a maximum of 31 letters or digits (0 through 9). Some processors accept the use of the special characters currency symbol $ and underscore _ as part of variable names. Examples of valid variable names are

MASS
VOLUME
VELOCITYX
JOB3

Examples of invalid variable names are

5MASS
%VOLUME
@VELOCITYX
_JOB3

When declaring variables, several data types are used in Fortran. These data types are specified by means of dec laration statements. Declaration statements are nonexecutable; therefore, they must precede all executable statements. A variable name can be declared only once within a program unit. Multiple declaration of a variable name (i.e., using the same name in two declaration statements) is not allowed. Fortran has six data types and six corresponding declaration statements:

1. INTEGER
2. REAL
3. COMPLEX
4. DOUBLE PRECISION
5. LOGICAL
6. CHARACTER.


3.1  INTEGER STATEMENT

The INTEGER declaration statement is used to explicitly declare the data type of a variable as integer. An integer is an arithmetic value lacking a fractional component, for instance the value 3, or the value -25.

In the absence of an INTEGER statement, an integer variable is declared implicitly by the sole action of naming its variable name starting with either of the letters I, J, K, L, M, or N.

An example of the INTEGER statement is

INTEGER COUNTER,FILEID

This statement declares the variables COUNTER and FILEID to be of integer type. In the absence of this state ment, the variables COUNTER and FILEID would be subject to the rules governing implicit type declaration. Since they start with the letters C and F, they would not be assumed to be integers.

The variables declared using an INTEGER statement can have a 2- or 4-byte wordsize, specified during program compilation in a processor-dependent mode. If no word size is specified, the default value is 4 bytes. The range of values that an integer variable can take (from largest negative to largest positive) is processor dependent.

Some processors accept the following alternate forms of the INTEGER statement:

INTEGER*2 COUNTER,FILEID
INTEGER*4 COUNTER,FILEID

These statements explicitly state the wordsize as 2 or 4 bytes, overriding the wordsize specified during program compilation, if any. Note that processors may assign an initial value of zero (0) to all integer variables.

Tips regarding allowable range of values:

  • Check your operating system's documentation for your computer's wordlength, and for the range of values allowed for the various data types.
  • The range for integer constants is typically from -2n-1 to +2n-1-1, where n is the wordlength. Therefore, the range of a 16-bit integer is from -32768 to +32767. Likewise, a 32-bit integer ranges from -2147483647 to +2147483646. Values outside of these ranges are not allowed.

3.2  REAL STATEMENT

The REAL declaration statement is used to explicitly declare the data type of a variable name as real. Real arithmetic values can take fractional components, such as 2.468, or -135.79.

In the absence of a REAL statement, a real variable is declared implicitly by the sole action of naming its variable name starting with any letter other than I, J, K, L, M, or N.

An example of the REAL statement is

REAL LENGTH,MASS

This statement declares the variables LENGTH and MASS to be of real type. In the absence of this statement, the variables LENGTH and MASS would be subject to the rules governing implicit type declaration. In this case, since they start with the letters L and M, they would be assumed to be integers.

Typically, the variables declared using a REAL state ment have a 4-byte wordsize. The approximate range of values that a real variable can take is processor dependent. The precision of a real value (i.e., the number of significant digits) is closely linked to the wordsize. A 4-byte wordsize usually has a precision of 7 digits.

Some processors accept the following alternate forms of the REAL statement:

REAL*4 LENGTH,MASS
REAL*8 LENGTH,MASS
REAL*16 LENGTH,MASS

These statements explicitly state the wordsize as being either 4, 8, or 16 bytes. In this case, the statement REAL*4 amounts to single precision, accomplishing the same purpose as the REAL statement.

REAL*8 amounts to double precision, accomplishing the same as the DOUBLE PRECISION statement (Section 3.5). REAL*16 amounts to quadruple precision, about 4 times that of single precision. Note that processors may assign an initial value of zero (0.0) to all real variables.

Programming tips for naming variables:
  • I, J, K, L, M, and N are used to implicitly start INTEGER variables; all other letters are used to implicitly start REAL variables.
  • An implicit declaration of an integer variable COUNTER can be accomplished by naming it instead KCOUNTER or KOUNTER.
  • An implicit declaration of a real variable LENGTH can be accomplished by naming it instead XLENGTH.

 
3.3  COMPLEX STATEMENT

The COMPLEX declaration statement declares the data type of a variable name as complex. A complex data type consists of a pair of real values representing the complex number (a + bi), with a being the real part and b the imaginary part. In Fortran, the two real numbers are separated by a comma and enclosed in parentheses. For example, the complex number 3 + 4i is written as (3.,4.)

An example of the COMPLEX statement is

COMPLEX C1,C2,C3

which declares the variable names C1, C2, and C3 to be of complex type.

Typically, the real and imaginary parts of a complex value have each a 4-byte wordsize, which usually has a precision of 7 digits. Unlike integer and real variables, which may be declared implicitly, complex variables may only be declared explicitly, by means of the COMPLEX statement. Note that a processor may assign an initial value of zero (0.0) to both real and imaginary parts of all complex variables.

The following example illustrates the declaration of complex variables. The assignment of complex variables may be done in two ways: (1) using a DATA statement, which is more permanent, and (2) using an arithmetic assignment statement.

Example: Complex Declaration and Assignment

C-----COMPLEX DECLARATION STATEMENTS 

      COMPLEX C1,C2,C3 

C-----COMPLEX VARIABLE ASSIGNMENT USING DATA

      DATA C1,C2 /(3.,4.5),(3.5,6.7)/ 

C-----COMPLEX VARIABLE ASSIGNMENT USING

C-----ARITHMETIC ASSIGNMENT STATEMENT 

      C3= (23.,-34.)

Complex Unformatted Input/Output

In unformatted input, complex values (two real numbers, separated by a comma and enclosed in parenthesis) should be separated among themselves either by: (1) a comma, (2) one or more blank spaces, or (3) a combination of a comma and one or more blank spaces in any or der. In unformatted output, the characteristics of complex values are processor dependent.

 
3.4  DOUBLE PRECISION STATEMENT

The DOUBLE PRECISION declaration statement declares the data type of a variable name as double precision. A double-precision variable is stored using twice as many bytes as the single-precision real variable. For instance, if a real variable is stored in 4 bytes (REAL*4), a double- precision variable is stored in 8 bytes (REAL*8).

An example of the DOUBLE PRECISION statement is

DOUBLE PRECISION A,B,C

where A, B, and C are double-precision variable names.

Typically, the variables declared using a DOUBLE PRECISION statement have an 8-byte wordsize, which usually has a precision of 15 to 16 digits.

Unlike integer and real variables, which may be declared implicitly, double precision variables may only be declared explicitly, by means of the DOUBLE PRECISION statement. Note that a processor may assign an initial value of zero (0.) to all double precision variables. In FORTRAN, the double-precision zero is written as 0.0D0, in which D stands for "times ten to the power...", i.e., 0.0 X 100. Likewise, 5.3D6 stands for 5.3 times ten to sixth power (that is, 5300000.00000000). The declaration and assignment of DOUBLE PRECISION variables is shown in the following example.

Example: Double-Precision Variable Declaration and Assignment

C234567890

C-----DOUBLE PRECISION DECLARATION STATEMENT 

      DOUBLE PRECISION A,B,C 

C-----DOUBLE PRECISION ASSIGNMENT USING

C-----DATA STATEMENT 

      DATA A,B /0.123456789D35,0.987654321D-29/

C-----DOUBLE PRECISION ASSIGNMENT USING

C-----ARITHMETIC ASSIGNMENT STATEMENT

      C= 0.2468D-22 

Double-precision Unformatted Input/Output

In unformatted input, double-precision values should be separated among themselves either by: (1) a comma, (2) one or more blank spaces, or (3) a combination of a comma and one or more blank spaces in any order. In unformatted output, the characteristics of complex values are processor dependent.

 
3.5  LOGICAL STATEMENT

The LOGICAL declaration statement declares the data type of a variable name as logical, i.e., a variable that can take a value of either true (.TRUE.) or false (.FALSE.). The delimiting periods encompassing the values TRUE and FALSE are required.

An example of the LOGICAL statement is

LOGICAL SWITCH1,SWITCH2

where SWITCH1 and SWITCH2 as logical variable names; thereby, admitting only the logical values .TRUE. or .FALSE. Logical variables may only be declared explicitly. Some processors assign an initial value of .FALSE. to all logical variables.

The declaration and assignment of logical variables is shown in the following example.

Example: Logical Variable Declaration and Assignment

C234567890 

C-----LOGICAL DECLARATION STATEMENT

      LOGICAL SWITCH1,SWITCH2

C-----LOGICAL ASSIGNMENT WITH DATA STATEMENT 

      DATA SWITCH1 /.TRUE./ 

C-----LOGICAL ASSIGNMENT WITH 

C-----LOGICAL ASSIGNMENT STATEMENT 

      SWITCH2= .FALSE.

Logical Unformatted Input/Output

In unformatted input, logical values should be sepa rated among themselves either by: (1) a comma, (2) one or more blank spaces, or (3) a combination of a comma and one or more blank spaces in any order. The logical values themselves can be .TRUE. or .FALSE., or .T. or .F., T or F (or any other word starting with either T or F). In unformatted output, logical values are written as either T or F.

 
3.6  CHARACTER STATEMENT

The CHARACTER declaration statement declares the data type of a variable name as character, i.e., one whose value is a character constant (also referred to as a character string).

An example of the CHARACTER statement is

CHARACTER*15 NAME,ADDRESS,SSNO

This statement declares the variables NAME, ADDRESS, and SSNO as character. Each of these variables can have up to 15 characters, as indicated by the length specifier immediately following the * after CHARACTER.

Alternatively, the statement

CHARACTER*15 NAME,ADDRESS,SSNO*11

declares these variables of character type, with length equal to 15 characters, excepting SSNO, whose length is equal to 11 characters. It is seen that the length specifier following the * after CHARACTER applies to all listed variables, unless it is overridden by affecting one or more variables with their own lengths. In addition, if the length specifier following CHARACTER is omitted, a default value equal to 1 is assumed. For example, in

CHARACTER ABC,DEF,GHI*5

the character variables ABC and DEF have length equal to 1, and the variable GHI has length equal to 5.

Example: Character Variable Declaration and Assignment

C234567890 

C-----CHARACTER VARIABLE DECLARATION 

      CHARACTER*15 NAME,ADDRESS,SSNO*11 

C-----CHARACTER ASSIGNMENT USING DATA STATEMENT

      DATA NAME, ADDRESS 

     1/'JOHN DOE','30 F STREET, NW'/ 

C-----CHARACTER ASSIGNMENT STATEMENT 

      SSNO= '555-32-9876' 

Character variables remain unfilled until an assignment is made during program program execution. It is not necessary that the character value fill the entire space allocated by the CHARACTER statement. In the preceding example, the variable NAME has been assigned the value JOHN DOEbbbbbbb, and it has 7 trailing blank spaces, represented here with the letter b.

Character Variable Input/Output Example Program

In data management, it is important to know how to read and write CHARACTER variables. The following exam ple program reads three character variables NAME, AD DRESS, and SSNO, each having a maximum length of 15 characters, from a file named CHARACTER.DAT using at least one and at most three unformatted record(s). It proceeds to write them to a file named CHARACTER.OUT using three separate unformatted records. In unformatted input, the character constants should be en closed by apostrophes and separated among themselves either by: (1) a comma, (2) one or more blank spaces, or (3) a combination of a comma and one or more blank spaces in any order.

Example Program: Character Variable Input/Output

C234567890 

      PROGRAM CHARACTER_IO

      CHARACTER*15 NAME,ADDRESS,SSNO 

      OPEN(5,FILE='CHARACTER.DAT',STATUS='UNKNOWN') 

      OPEN(6,FILE='CHARACTER.OUT',STATUS='UNKNOWN')

      READ(5,*) NAME,ADDRESS,SSNO 

      WRITE(6,*) NAME

      WRITE(6,*) ADDRESS

      WRITE(6,*) SSNO 

      END

Character Variable Example Program: Printing a Banner

The example below prints the following banner to the
screen:


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 		
X    X    X    X    X    X    X    X		
X    X    X    X    X    X    X    X		
X    X    X    X    X    X    X    X		
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 		

Character Example Program: Printing a Banner

C234567890

      PROGRAM BANNER 

      CHARACTER*36 XSOLID,XSPACE 

      DATA XSOLID/ 

     1'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'/ 

      DATA XSPACE/ 

     1'X    X    X    X    X    X    X    X'/

      WRITE(6,*) XSOLID 

      WRITE(6,*) XSPACE 

      WRITE(6,*) XSPACE 

      WRITE(6,*) XSPACE

      WRITE(6,*) XSOLID 

      END

Note the features of this program:

  •  The two character variables (XSOLID AND XSPACE) are declared using a CHARACTER statement, each of 36-character length. 
  • The character variables are assigned using DATA state ments. 
  • The WRITE statements direct output to the default output device, the screen.
 
3.7  DECLARATION STATEMENTS IN FORTRAN 90 

In general, data types can be either (1) intrinsic, i.e., predefined by the language, or (2) derived, i.e., subject to definition by the programmer. In FORTRAN 77, all data types and their declaration statements are intrinsic (see Sections 3.1 to 3.6). Fortran 90 allows the use of either intrinsic or derived data types. 

Fortran 90 has five intrinsic data types: (1) integer, (2) real, (3) complex, (4) character, and (5) logical. Note that the double precision data type of FORTRAN 77 is missing from this list. Also, note that since FORTRAN 77 is contained entirely within Fortran 90, the DOUBLE PRECISION statement can still be used with Fortran 90.

In Fortran 90, derived data types are defined with a TYPE statement. A derived data type can have one or more components. Each of the component can be in itself either intrinsic or derived. A derived data type needs a TYPE definition to supply the name of the derived data type, and the data type(s) and name(s) of its components.

For example, if the complex type were not intrinsic but had to be derived, a type definition would be required to supply the name "complex" and declare two components, each of real type, as follows. 

TYPE COMPLEX
REAL RCOMP
REAL ICOMP
END TYPE COMPLEX

Note that the type definition is written in several lines, with optional indentation of the components. The TYPE and END TYPE statements encompass the components. 

To define a double precision data type, Fotran 90 does not use the TYPE statement, but rather, it uses the intrinsic type REAL affected with a KIND parameter, as in the fol lowing example

REAL (KIND (0.0D0)) DPMASS 

where the variable DPMASS is declared as double preci sion. The value of the KIND parameter (0.0D0) represents double precision. On the other hand, the statement 

REAL (KIND (0.0)) MASS 

defines a real single-precision variable MASS, as does the statement 

REAL MASS

 The following is an example of a derived data type having two components, an integer and a character. 

TYPE PERSON
CHARACTER (LEN=30) NAME
INTEGER AGE
END TYPE PERSON 

This example defines a data type PERSON, having two components: (1) a character NAME, of length 30 spaces, and (2) an integer AGE. 

The variable declaration of the previously defined derived-type PERSON is: 

TYPE (PERSON):: EMPLOYEE 

A corresponding derived-type constant expression is 

PERSON ('JOHN SMITH',28) 

such that 

EMPLOYEE = PERSON ('JOHN SMITH',28)

3.8  SUMMARY 

This chapter introduces six statements: INTEGER, REAL, COMPLEX, DOUBLE PRECISION, LOGICAL and CHARACTER. These declaration statements declare the data types of corresponding variables. They are nonexecutable; therefore, they should precede all executable statements in a program unit. 

The use of the INTEGER and REAL statements is not absolutely necessary; their usage can be avoided by following certain simple rules in naming variables. All other declaration statements are absolutely necessary to declare their respective data types. 

 
CHAPTER 3 -- PROBLEMS

  1. Write a program to do the following tasks: (1) declare a logical variable FLAG; (2) initialize it with the value .FALSE. using a DATA statement; (3) write this initial value to an output file FLAG.OLD; (4) assign FLAG a new value .TRUE.; and (5) write this new value to an output file FLAG.NEW. Look at the output files to examine the results.
  2. Write a program to do the following tasks: (1) using a DATA statement, initialize the double precision variables DA= 0.1234567890123456D-16, and DB= 0.9876543210987654D16; (2) multiply (DA*DB) and divide them (DA/DB) to generate the double precision variables DC and DD, respectively; and (3) write the results directly to the screen, preceded by an appropriate character label. 
  3. Using the CHARACTER statement, write a program to write the following banner to a file E_BANNER.OUT:

    EEEEEEEEEEE 

    EEEEEEEEEEE 

    E

    EEEEEEEEEEE 

  4. Write a program to do the following tasks: (1) initialize two variables X= 0.1234567890123456, and Y= 0.1234567 as double precision; (2) calculate Z= X/(X - Y) using single-precision data types for X, Y, and Z; (3) calculate Z using double precision data types for X, Y, and Z; (4) calculate the difference between the Z values obtained using double and single precision; and (5) write the result directly to the screen. What can you surmise from this exercise? Assuming the Z calculated using double precision as the correct answer, what is the precision of the Z calculated using single precision? [Precision is the number of correct or significant digits]. 
  5. Write a program to do the following tasks: (1) initialize two variables X= 0.2468, and Y= 0.24675 as single precision; (2) calculate Z= Y/(X - Y)0.95 using single-precision (real) data types for X, Y, and Z; (3) calculate Z using double precision data types for X, Y, and Z; (4) calculate the difference between the Z values obtained using single and double precision; and (5) write the result directly to the screen. Assuming the Z calculated using double precision as correct, what is the precision of the Z calculated using single precision? [Precision is the number of correct or significant digits]. 
  6. Write a program to do the following tasks: (1) declare a logical variable FLAG; (2) initialize it with the value .FALSE. using a DATA statement; (3) write this initial value directly to the screen, using a label such as THE INITIAL VALUE OF FLAG IS ; (4) assign FLAG a new value .TRUE.; and (5) write this new value directly to the screen using a label such as THE UPDATED VALUE OF FLAG IS .
  7. Write a program that initializes the value π with 15 significant digits (π= 3.141592653589793). Then, it multiplies it by 2., and writes the value 2π to an output file TWOPI.OUT, preceded by an appropriate label.
  8. Using the CHARACTER statement, write a program to write the following banner to a file X_BANNER.OUT.

    X         X

     X       X 

      X     X 

       X   X 

        X X 

         X

        X X

       X   X

      X     X

     X       X

    X         X

  9. Write a program to calculate the value of eγ, in which e is the natural log base and γ is Euler's constant, using single and double precision. Use e= 2.718281828459045, and γ= 0.5772156649015328. How many correct or significant digits does single precision have in this example? 
  10. Write an interactive program to calculate the hyperbolic tangent of an angle x in radians. Use: tanh x= (ex - e-x)/(ex + e-x). Test your program using: (a) x= π; and (b) x= π/2. 
  11. Repeat Problem 10, this time calculating both the hyperbolic tangent and the hyperbolic cotangent. Calculate and write the hyperbolic tangent first. Then, calculate and write the hyperbolic cotangent. Test your program using: (a) x= 2π; and (b) x= 0. What happens for coth 0? 
  12. Write a program to do the following tasks: (1) initialize two complex variables COMP1= (1.,3.) and COMP2= (2.,4.); (2) sum, subtract, multiply, and divide them; and (3) write the results directly to the screen, preceded with appropriate labels. 
  13. Find out if your processor can accept a double-precision complex data type (either COMPLEX*16 or DOUBLE COMPLEX statement). If so, repeat Problem 12 using instead DCOMP1= (1.23456789, 3.57913579), and DCOMP2= (2.46802468, 4.68024680). Write the results directly to the screen, preceded with appropriate labels. 
  14. Using the CHARACTER statement, write a program to print the following geometric design of a tic-tac-toe board:

         X     X

         O     O

         X     X

    XOXOXOXOXOXOXOXOX

         X     X

         O     O

         X     X

    XOXOXOXOXOXOXOXOX

         X     X

         O     O

         X     X

  15. Using CHARACTER and DATA statements, write a program to print the following numeric design directly to the screen:

    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO


       http://ponce.sdsu.edu/fortran_book_03.html 090311