Previous | Contents | Index |
This chapter describes:
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.
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.
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 |
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:
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. |
On the scope of names, see Section 15.2.
2.2 Character Sets
HP Fortran supports the following 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 |
Uppercase and lowercase letters are treated as equivalent when used to specify program behavior (except in character constants and Hollerith constants).
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:
Table 2-2 summarizes characters used as 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 |
Source code can be written so that it is useable for all source forms (see Section 2.3.3).
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.
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:
INTEGER NUM GO TO 40 20 DO K=1,8 |
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.
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.
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.
On the general rules for all source forms, see Section 2.3.
Previous | Next | Contents | Index |