[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index


Chapter 2
Program Structure, Characters, and Source Forms

This chapter describes:

2.1 Program Structure

A Fortran program consists of one or more program units. A program unit is usually a sequence of statements that define the data environment and the steps necessary to perform calculations; it is terminated by an END statement.

A program unit can be either a main program, an external subprogram, a module, or a block data program unit. An executable program contains one main program, and, optionally, any number of the other kinds of program units. Program units can be separately compiled.

An external subprogram is a function or subroutine that is not contained within a main program, a module, or another subprogram. It defines a procedure to be performed and can be invoked from other program units of the Fortran program. Modules and block data program units are not executable, so they are not considered to be procedures. (Modules can contain module procedures, though, which are executable.)

Modules contain definitions that can be made accessible to other program units: data and type definitions, definitions of procedures (called module subprograms), and procedure interfaces. Module subprograms can be either functions or subroutines. They can be invoked by other module subprograms in the module, or by other program units that access the module.

A block data program unit specifies initial values for data objects in named common blocks. In Fortran 95/90, this type of program unit can be replaced by a module program unit.

Main programs, external subprograms, and module subprograms can contain internal subprograms. The entity that contains the internal subprogram is its host. Internal subprograms can be invoked only by their host or by other internal subprograms in the same host. Internal subprograms must not contain internal subprograms.

For More Information:

On program units and procedures, see Chapter 8.

2.1.1 Statements

Program statements are grouped into two general classes: executable and nonexecutable. An executable statement specifies an action to be performed. A nonexecutable statement describes program attributes, such as the arrangement and characteristics of data, as well as editing and data-conversion information.

Order of Statements in a Program Unit

Figure 2-1 shows the required order of statements in a Fortran program unit. In this figure, vertical lines separate statement types that can be interspersed. For example, you can intersperse DATA statements with executable constructs.

Horizontal lines indicate statement types that cannot be interspersed. For example, you cannot intersperse DATA statements with CONTAINS statements.

Figure 2-1 Required Order of Statements


Note that directives and the OPTIONS statement are HP Fortran language extensions.

PUBLIC and PRIVATE statements are only allowed in the scoping units of modules. In Fortran 95/90, NAMELIST statements can appear only among specification statements. However, HP Fortran allows them to also appear among executable statements. Table 2-1 shows other statements restricted from different types of scoping units.

Table 2-1 Statements Restricted in Scoping Units
Scoping Unit Restricted Statements
Main program ENTRY and RETURN statements
Module 1 ENTRY, FORMAT, OPTIONAL, and INTENT statements, statement functions, and executable statements
Block data program unit CONTAINS, ENTRY, and FORMAT statements, interface blocks, statement functions, and executable statements
Internal subprogram CONTAINS and ENTRY statements
Interface body CONTAINS, DATA, ENTRY, SAVE, and FORMAT statements, statement functions, and executable statements

1The scoping unit of a module does not include any module subprograms that the module contains.

For More Information:

On scoping units, see Section 15.2.

2.1.2 Names

Names identify entities within a Fortran program unit (such as variables, function results, common blocks, named constants, procedures, program units, namelist groups, and dummy arguments). In FORTRAN 77, names were called "symbolic names."

A name can contain letters, digits, underscores (_), and the dollar sign ($) special character. The first character must be a letter or a dollar sign.

In Fortran 95/90, a name can contain up to 31 characters. HP Fortran allows names up to 63 characters.

The length of a module name (in MODULE and USE statements) may be restricted by your file system.

In an executable program, the names of the following entities are global and must be unique in the entire program:

  • Program units
  • External procedures
  • Common blocks
  • Modules

Examples

The following examples demonstrate valid and invalid names:

Valid  
NUMBER  
FIND_IT  
X  
Invalid Explanation
5Q Begins with a numeral.
B.4 Contains a special character other than _ or $.
_WRONG Begins with an underscore.

For More Information:

On the scope of names, see Section 15.2.

2.2 Character Sets

HP Fortran supports the following characters:

  • The Fortran 95/90 character set which consists of the following:
    • All uppercase and lowercase letters (A through Z and a through z)
    • The numerals 0 through 9
    • The underscore (_)
    • The following special characters:

    Character Name Character Name
    <ucDelta symbol> or <Tab> Blank (space) or tab : Colon
    = Equal sign ! Exclamation point
    + Plus sign " Quotation mark
    -- Minus sign % Percent sign
    * Asterisk & Ampersand
    / Slash ; Semicolon
    ( Left parenthesis < Less than
    ) Right parenthesis > Greater than
    , Comma ? Question mark
    . Period (decimal point) $ Dollar sign (currency symbol)
    ' Apostrophe    
  • Other printable characters
    Printable characters include the tab character (09 hex), ASCII characters with codes in the range 20(hex) through 7E(hex), and characters in the DEC Multinational Extension to the ASCII Character Set with codes in the range A1(hex) through FE(hex).
    Printable characters that are not in the Fortran 95/90 character set can only appear in comments, character constants, Hollerith constants, character string edit descriptors, and input/output records.

Uppercase and lowercase letters are treated as equivalent when used to specify program behavior (except in character constants and Hollerith constants).

For More Information:

On the ASCII and DEC Multinational character sets, see Appendix C.

2.3 Source Forms

Within a program, source code can be in free, fixed, or tab form. Fixed or tab forms must not be mixed with free form in the same source program, but different source forms can be used in different source programs.

All source forms allow lowercase characters to be used as an alternative to uppercase characters.

Several characters are indicators in source code (unless they appear within a comment or a Hollerith or character constant). The following are rules for indicators in all source forms:

  • Comment indicator
    A comment indicator can precede the first statement of a program unit and appear anywhere within a program unit. If the comment indicator appears within a source line, the comment extends to the end of the line.
    An all blank line is also a comment line.
    Comments have no effect on the interpretation of the program unit.
    For more information on comment indicators in free source form, see Section 2.3.1; in fixed and tab source forms, see Section 2.3.2.
  • Statement separator
    More than one statement (or partial statement) can appear on a single source line if a statement separator is placed between the statements. The statement separator is a semicolon character (;).
    Consecutive semicolons (with or without intervening blanks) are considered to be one semicolon.
    If a semicolon is the last character on a line, or the last character before a comment, it is ignored.
  • Continuation indicator
    A statement can be continued for more than one line by placing a continuation indicator on the line. HP Fortran allows up to 511 continuation lines in a source program.
    Comments can occur within a continued statement, but comment lines cannot be continued.
    Within a program unit, the END statement cannot be continued, and no other statement in the program unit can have an initial line that appears to be the program unit END statement.
    For more information on continuation indicators in free source form, see Section 2.3.1; in fixed and tab source forms, see Section 2.3.2.

Table 2-2 summarizes characters used as indicators in source forms:

Table 2-2 Indicators in Source Forms
Source Item Indicator1 Source Form Position
Comment ! All forms Anywhere in source code
       
Comment line ! Free At the beginning of the source line
  !, C, or * Fixed In column 1
    Tab In column 1
       
Continuation line 2 & Free At the end of the source line
  Any character except zero or blank Fixed In column 6
  Any digit except zero Tab After the first tab
       
Statement separator ; All forms Between statements on the same line
       
Statement label 1 to 5 decimal digits Free Before a statement
    Fixed In columns 1 through 5
    Tab Before the first tab
       
A debugging statement 3 D Fixed In column 1
    Tab In column 1

1If the character appears in a Hollerith or character constant, it is not an indicator and is ignored.
2For all forms, up to 511 continuation lines are allowed.
3For fixed and tab forms only.

Source code can be written so that it is useable for all source forms (see Section 2.3.3).

Statement Labels

A statement label (or statement number) identifies a statement so that other statements can refer to it, either to get information or to transfer control. A label can precede any statement that is not part of another statement.

A statement label must be one to five decimal digits long; blanks and leading zeros are ignored. An all-zero statement label is invalid, and a blank statement cannot be labeled.

Labeled FORMAT and labeled executable statements are the only statements that can be referred to by other statements. FORMAT statements are referred to only in the format specifier of an I/O statement or in an ASSIGN statement. Two statements within a scoping unit cannot have the same label.

For More Information:

On labels in free source form, see Section 2.3.1; in fixed or tab source form, see Section 2.3.2.

2.3.1 Free Source Form

In free source form, statements are not limited to specific positions on a source line. In Fortran 95/90, a free form source line can contain from 0 to 132 characters. HP Fortran allows the line to be of any length.

Blank characters are significant in free source form. The following are rules for blank characters:

  • Blank characters must not appear in lexical tokens, except within a character context. For example, there can be no blanks between the exponentiation operator **. Blank characters can be used freely between lexical tokens to improve legibility.
  • Blank characters must be used to separate names, constants, or labels from adjacent keywords, names, constants, or labels. For example, consider the following statements:


    INTEGER NUM
    GO TO 40
    20 DO K=1,8
    

    The blanks are required after INTEGER, TO, 20, and DO.
  • Some adjacent keywords must have one or more blank characters between them. Others do not require any; for example, BLOCK DATA can also be spelled BLOCKDATA. The following list shows which keywords have optional or required blanks:
    Optional Blanks Required Blanks
    BLOCK DATA CASE DEFAULT
    DOUBLE COMPLEX DO WHILE
    DOUBLE PRECISION IMPLICIT type-specifier
    ELSE IF IMPLICIT NONE
    END BLOCK DATA INTERFACE ASSIGNMENT
    END DO INTERFACE OPERATOR
    END FILE MODULE PROCEDURE
    END FORALL RECURSIVE FUNCTION
    END FUNCTION RECURSIVE SUBROUTINE
    END IF RECURSIVE type-specifier FUNCTION
    END INTERFACE type-specifier FUNCTION
    END MODULE type-specifier RECURSIVE FUNCTION
    END PROGRAM  
    END SELECT  
    END SUBROUTINE  
    END TYPE  
    END WHERE  
    GO TO  
    IN OUT  
    SELECT CASE  

For information on statement separators (;) in all forms, see Section 2.3.

Comment Indicator

In free source form, the exclamation point character (!) indicates a comment if it is within a source line, or a comment line if it is the first character in a source line.

Continuation Indicator

In free source form, the ampersand character (&) indicates a continuation line (unless it appears in a Hollerith or character constant, or within a comment). The continuation line is the first noncomment line following the ampersand. Although Fortran 95/90 permits up to 39 continuation lines in free-form programs, HP Fortran allows up to 511 continuation lines.

The following shows a continued statement:


TCOSH(Y) = EXP(Y) + &        ! The initial statement line
           EXP(-Y)           ! A continuation line

If the first nonblank character on the next noncomment line is an ampersand, the statement continues at the character following the ampersand. For example, the preceding example can be written as follows:


TCOSH(Y) = EXP(Y) + &
          & EXP(-Y)

If a lexical token must be continued, the first nonblank character on the next noncomment line must be an ampersand followed immediately by the rest of the token. For example:


TCOSH(Y) = EXP(Y) + EX&
          &P(-Y)

If you continue a character constant, an ampersand must be the first non-blank character of the continued line; the statement continues with the next character following the ampersand. For example:


ADVERTISER = "Davis, O'Brien, Chalmers & Peter&
                 &son"
ARCHITECT  = "O'Connor, Emerson, and Davis&
                 & Associates"

If the ampersand is omitted on the continued line, the statement continues with the first non-blank character in the continued line. So, in the preceding example, the whitespace before "Associates" would be ignored.

The ampersand cannot be the only nonblank character in a line, or the only nonblank character before a comment; an ampersand in a comment is ignored.

For More Information:

On the general rules for all source forms, see Section 2.3.


Previous Next Contents Index