Previous Table of Contents Next


1.2.4.7. The Logical Type

The logical type is used to represent the two truth values true and false.

A logical constant is either .true. or .false., possibly followed by an underscore and a kind parameter.

The operators that may be used to combine logical values are .not., .and., .or., .eqv., and .neqv..

To give one simple example, the value of .false. .eqv. .false. is true.

1.2.4.8. The Character Type

The character type is used to represent strings of characters. The form of a character constant is a sequence of any characters representable in the computer delimited by quotation marks. If a quotation mark is to occur in the character string, it is represented by two quotation marks with no intervening characters:

   “He said, ““Don’t tread on me.”””

There is only one character operator that produces a character result: concatenation. For example, the value of “John Q.” // “Public” is the string “John Q.Public”, with no blank after the period.

1.2.5. Kind Parameters

Kind parameters provide a way to parameterize the selection of different possible machine representations for each of the intrinsic data types. If the programmer is careful, this provides a mechanism for making the selection of numeric precision and range portable.

Each intrinsic data type (integer, real, complex, characters, and logical) has a parameter, called its kind parameter, associated with it. A kind parameter is intended to designate a machine representation for a particular data type. As an example, an implementation might have three real kinds, informally known as single, double, and quadruple precision.

The kind parameter is an integer. These numbers are processor dependent so that kind parameters 1, 2, and 3 might be single, double, and quadruple precision; on a different system, kind parameters 4, 8, and 16 could be used for the same things.

There are at least two real and complex kinds and at least one kind for the other data types.

Note that the value of the kind parameter has nothing to do with the number of decimal digits of precision or range.

The intrinsic functions selected_int_kind and selected_real_kind can be used to select an appropriate kind for a variable or a named constant. These functions provide the means for making a program portable in cases where values need to be computed with a certain specified precision that may use single precision on one machine but require double precision on another machine.

When a kind parameter is a part of another constant, it follows the underscore at the end of the constant (except character, in which case it precedes the underscore and constant):

   12345_short
   1.345_very_precise
   .true._enough
   ascii_”first_name”

1.2.6. Parameters/Named Constants

A parameter is a named constant. Each parameter must be declared in a type statement. Type statements appear between the program statement and the beginning of the executable part of the program. Type statements also are used to give names to variables and indicate their data type.

Each parameter declaration consists of a keyword specifying an intrinsic type, followed by a comma and the keyword parameter, followed by two colons. To the right of the double colon is a list of names, each followed by an assignment and the expression giving the parameter value. The initialization assignments are separated by commas:

   real, parameter :: pi = 3.14159, e = 2.71828
   integer, parameter :: number_of_states = 50, &
                         number_of_senators = 2 * number_of_states

The value of a parameter is fixed by its declaration and cannot change during execution of a program.

1.2.7. Rules for Names

number_of_states and number_of_senators are names. The following are the rules for names of parameters as well as all other names in a program:

  The first character of the name must be a letter.
  The remaining characters may be any mixture of letters, digits, or underscore characters (_).
  There may be at most 31 characters in a name.

1.2.8. Variables

The value of a parameter is fixed by its declaration and cannot change during execution of a program. On the other hand, if the keyword parameter is omitted, the objects being declared become variables and their values can be changed at any time.

Thus, the following declares count to be an integer variable:

   integer :: count

Variables may have a particular hardware representation by putting kind= followed by a named constant in parentheses after the keyword representing the data type:

   integer, parameter :: more_precision = 2
   real (kind = more_precision) :: dpq, x, long

A character variable also has a length. The keyword character may be followed by len= and an integer value indicating the number of characters in the character string in parentheses:

   character (len = 20) :: name

Instead of an integer, the length of a dummy argument of type character may be * (meaning “assumed from the actual argument”).

1.2.9. Attributes

In addition to the data type, a variable may be given several other attributes in a type declaration statement. Examples are pointer and save. Table 1.2 indicates these attributes and which of them make sense together.

Table 1.2. Attribute compatibility.

If two attributes can appear in the same type declaration statement, a check mark ([check]) appears at their intersection in the chart. An × indicates incompatibility.

1.2.10. Intrinsic Functions

There are many built-in or intrinsic functions and a few built-in subroutines. To use the functions, simply type the name of the function followed by the arguments to the function, enclosed in parentheses. For example, abs(x) produces the absolute value of x, and max(a,b,c) yields the maximum of the values of a, b, and c.

Two of the more commonly used subroutines are date_and_time and random_number.


Previous Table of Contents Next