FORTRAN Mini-Manual

Prior to installing MVT and MVS under Hercules on my personal computer, my only experience with FORTRAN was in a two week period that was part of an Introduction to Computers course in 1974.  Since I now have a FORTRAN compiler available, I decided to go to a couple of libraries and see what I could teach myself beyond what I remembered of the very limited amount I had been shown back then.  

In order to organize the notes I made as I scanned the few books I found on Fortran IV, I transcribed them into a Word document.  And, since I had already gone that far, I decided to put them up here so that someone else might benefit from my efforts.  It is not going to be anything on the scope of what I have written on RPG, but it might be considered a mini-reference manual on FORTRAN.

For quick access, the following FORTRAN statements or language elements are bookmarked:

 

Statement Coding Conventions

FORTRAN is said to be a free-form language, which means that mostly there are no rules constraining how a statement is spread over the card images used to record the program.  The compiler will even correctly parse most statements if all spaces are eliminated that makes the program more human readable, for example the spaces separating verbs and operands.  Still, there are four fixed areas on the coding statement:

Columns

Area Description

1 - 5

label field

6

continuation field

7 - 72

statement field

73 - 80

identification field (ignored by the compiler)

The compiler ignores any line in which there is a C in column one, but the line will be printed on the program listing, therefore providing the means to imbed comments in the program statements.

In FORTRAN, the label field contains a number, which may be from one to five digits.  Leading and trailing blanks are ignored by the compiler, so it is insignificant whether the number is right or left justified in the label field area.  The label field is used on FORMAT statements to associate them with READ and/or WRITE statements and also on statements which are the target of GO TO statements, used to redirect the program flow.

 

Data Types

Since FORTRAN is designed to deal with numerical applications, all data types in the language are for storing numbers.  The four data types are:  INTEGER, REAL, DOUBLE PRECISION, and COMPLEX.  This doesn't mean that you cannot store and manipulate character information with a FORTRAN program, but there is no separate data type provided for this purpose.

REAL numbers are ordinary numbers with a decimal point.  DOUBLE PRECISION numbers are the same as REAL numbers, but are able to express a more precise (more decimal places) measurement.  COMPLEX numbers are used in very heavy mathematical applications, and consist of both a real component and an imaginary component.  Since such mathematics is way beyond my comprehension, I mostly ignored any discussion of COMPLEX variables and operations.  INTEGERs are numbers which are whole numbers, that is, no decimal places.

The only non-numeric data type is LOGICAL, which is used to contain the result of logical operations and will contain a value of TRUE or FALSE.

FORTRAN has an implicit data type mechanism, where the first character of the variable name determines the data type of the variable.  If the first character of the variable name is  I, J, K, L, M, or N, then the variable is an INTEGER.  Variables beginning with all other characters are assigned to the type REAL.  It is usually better to explicitly declare each variable name as the data type you expect the variable to contain with a type declaration statement near the beginning of the program.  The statement is of the form:

<data type> <variable list>

where <data type> can be INTEGER, REAL, DOUBLE PRECISION, COMPLEX, or LOGICAL and <variable list> is the name of one or more variables separated by commas.  For example:

INTEGER COUNT,DAY
REAL AMT,AVG,BAL
LOGICAL YES,NO

Alphanumeric Characters

There is no separate data type for handling alphanumeric characters in FORTRAN, but that doesn't mean that it cannot be done.  Alphanumeric characters may be stored in variables declared as any data type, but it is usual practice to use INTEGER type variables to store character data.

Because the word size of the machine upon which a program is run determines the storage capacity of a specific variable, in a FORTRAN program running under MVT or MVS on an emulated S/370, each INTEGER variable is capable of storing four alphanumeric characters.  This makes it somewhat awkward to manipulate data like a customer name, but it can be accomplished with a little effort.  Further discussion and examples of manipulating character data is deferred to the next sections on READ, WRITE, and FORMAT statements.

 

Variables

FORTRAN variable names may be from one to six characters.  A variable name may consist of letters and numbers only, no special characters, and the first character must be a letter.

As noted above (under DATA TYPES), there is an implicit data type associated with variables based upon the first character of the variable name unless explicitly overridden with a type declaration statement.  If the first character of a variable is I, J, K, L, M, or N, the variable is assigned the data type of INTEGER.  If the first character of a variable is any other letter, the variable is assigned the data type of REAL.

 

READ and WRITE Statements

The statements used to control input and output are so similar, I have grouped them here.  The format of the statements is:

READ (<unit number>, <format statement label>) <variable list>
WRITE (<unit number>, <format statement label>) <variable list>

Unit Numbers

The <unit number> associates the READ or WRITE statement with a physical device, which under MVT and MVS are represented in JCL as DD statements.  Unit number 5 is used to designate input read from card images, unit number 6 is used to designate output written to a printer, and unit number 7 is used to designate output written to card image (card punch device).  If you look at the standard FORTRAN procedures, you will see the DD statements for these unit numbers in the GO step:

//FT05F001 DD  DDNAME=SYSIN                                             
//FT06F001 DD  SYSOUT=A                                                 
//FT07F001 DD SYSOUT=B                                                  

In order to READ and WRITE data from magnetic tape and disk, unit numbers greater than 7 are specified.  However, I have never found a discussion of actually using FORTRAN IV to process magnetic tape/disk files.  In fact, in 1974 I abandoned FORTRAN for COBOL for just that reason.

Format Statement Label

The <format statement label> associates the READ or WRITE statement with a FORMAT statement, which describes the appearance and placement of the data to be read or written on the external media.  For the WRITE statement, the FORMAT statement specifies carriage control information and may also specify literal information to be interspersed with the data from the variable list.  In fact, for the WRITE statement, the variable list may be omitted and the entire output of the WRITE statement may be supplied as literals in the FORMAT statement.

The <format statement label> is a number corresponding to the identical number on a FORMAT statement, which may occur before or after the associated READ or WRITE statement in the program.  Most often, the FORMAT statement directly follows the associated READ or WRITE statement.  A single FORMAT statement may be associated with multiple READ or WRITE statements.  A single FORMAT statement could be associated with both a READ and a WRITE statement in the same program, but that would probably be unusual.

Variable List

The <variable list> contains the names of one or more variables separated by commas.

In the case of the READ statement, data, under the control of the FORMAT statement, is transferred from the data record into the storage location reserved for the variable.

In the case of the WRITE statement, data, under the control of the FORMAT statement, is transferred from the storage location named by the variable to the data record.  

It is possible to have WRITE statements that omit the variable list entirely, in which case all information transferred to the external media will come from literals contained in the FORMAT statement.  An example of when that might be the case would be the production of page or column heading information on a report.

 

FORMAT Statement

Because the FORMAT statement is the single FORTRAN statement that is used to describe all input and output, which includes controlling the type of translation to/from internal/external representation, it is the most complex of all of the FORTRAN statements.  The format of the statement is:

<format statement label> FORMAT (<data descriptor list>)

The <format statement label> is a one to five digit number that uniquely identifies a particular FORMAT statement within the program.  Using this statement label, it is possible for READ and/or WRITE statements to perform input and output under the control of the data descriptors included in the data descriptor list.

The <data descriptor list> is a list of one or more data descriptors, spacing descriptors, literal values, and repetition or grouping specifications.  Because the data descriptors are identical for both input (READ) and output (WRITE) operations, I will describe them first and then devote a specific section to the descriptors uniquely used for WRITE operations.

INTEGER Data Descriptor

The INTEGER data descriptor describes a string of numeric digits which are represented internally as an integer and are stored under a variable of type INTEGER.  The format of the data descriptor is:

I<w>

where <w> specifies the number of digits in the external representation of the number.

On INPUT, the number of digits may be fewer than the value specified by <w>, and the digits which are present may be either left or right justified in the field implied by the value of <w>.  The digits may be preceded by a sign (- or +) and, if present, the position occupied by the sign is counted as one of the positions included in the value of <w>.  No characters other than blanks, digits, or a sign are legal.  Any blanks in the field will be interpreted as zero digits.  Examples:

External (b = blank)                               I6                                     Internal

bb1042

1042

bbb-27

-27

-27bbb

-27000

bb+bb6

6

-3bb4b

-30040

bbbbbb

0

b-3+44

illegal for descriptor

bb3.14

illegal for descriptor

On OUTPUT, the value stored internally is converted into a string of digit characters.  If the value is negative, the digits will be preceded by a negative sign (-).  If the value does not use up all of the positions specified by the value of <w>, the number is right-justified in the field with blanks to the left.  If the value is too large to fit in a field of width <w>, a field of all asterisks (******) the width of <w> will be printed instead of the variable contents to indicate an overflow error.

Internal                               I6                                     External (b = blank)

1042

bb1042

-27

bbb-27

-2700

b-2700

6

bbbbb6

-30040

-30040

0

bbbbb0

-300400

******

3.14

******

REAL Data Descriptor

The REAL data descriptor describes a string of numeric digits which are represented internally as a floating point value and are stored under a variable of type REAL.  The format of the data descriptor is:

F<w>.<d>

where <w> specifies the number of digits in the external representation of the number, including the decimal point, and <d> specifies the number of digits which may be present to the right of the decimal point..

On INPUT, the external string of digits may be preceded by a sign (+ or -) and may contain a decimal point.  If the decimal point is not present, it is assumed to precede the <d> rightmost characters in the field (<d> must be smaller than <w>).  The number may be written in scientific notation (E notation).  All blank characters are interpreted as zeros.

External (b = blank)                               F6.2                                     Internal

bb3.14

3.14

b-2.79

-2.79

+b90.1

90.1

0.1234

0.1234

bbb372

3.72

bb372b

37.20

9001+1

900.1

2.1E-3

0.0021

On OUTPUT, the value stored internally is converted into a string of digit characters.  The value is rounded (not truncated) to <d> decimal places (digits beyond the decimal point).  If the string of characters is less than <w>, including the decimal point and a possible minus sign, then the number is right-justified and the field is filled with blanks to the left.  If the number is too large to fit into <w> character positions, a field of all asterisks (******) the width of <w> will be printed instead of the variable contents to indicate an overflow error.

Internal                               F6.2                                     External (b = blank)

3.14

bb3.14

-2.79

b-2.79

90.1

b90.10

49.2*10-3

bb0.05

49.2*10-6

bb0.00

0.0000

bb0.00

49.2*106

******

3927

illegal for descriptor

Scientific Notation (E notation) REAL Data Descriptor

REAL data may also be explicitly specified to be represented in scientific notation by using the E data descriptor.  The input effect is identical to that of the F data descriptor.  The format of the data descriptor is:

E<w>.<d>

where <w> specifies the number of digits in the external representation of the number, including the decimal point, and <d> specifies the number of digits which may be present to the right of the decimal point..

On INPUT, if the number is recorded on the input media in scientific notation, then the decimal point shift factor must be right-justified in the field; otherwise, the blanks following it will be interpreted as zeros, thus increasing the shift by factors of ten.  If a sign is included with the decimal point shift factor, as in 1.7E-6, and 4.932E+8, then the E may be omitted to save room in the data field.  In addition, the decimal point may be omitted from the number, but if so, it is implicitly placed <d> places to the left of the decimal point shift factor.

External (b = blank)                               E7.2                                     Internal

bb3.141

3.141

bbb3.14

3.14

bbbb314

3.14

3.14E00

3.14

3.14E-2

.0314

b314E-2

.0314

bb314-2

.0314

314b+2b

31.40*1020

On OUTPUT, the value stored internally is output in scientific notation, rounded to <d> significant digits.  The rightmost four spaces in the output field of width <w> are used for the decimal point shift factor printed in the form E+xx or E-xx, where each x is a single digit.  The number is printed in the <d>+3 spaces to the left of the decimal point shift factor in the form b0.<f> or -0.<f>, where <f> is a <d> digit unsigned INTERGER.  The leftmost <w> - (<d> + 7) spaces are left blank.  The value specified for <w> should be at least as large as <d> +7 to allow enough room for the number to be output.

Internal                                 E10.2                                 External (b = blank)

3.168

bb0.32E+01

492.1*10-23

bb0.49E-20

-3987.12

b-0.40E+04

Generalized REAL Descriptor

There are times when you wish to print out values with a certain number of significant digits.  The G data descriptor provides that capability.  The input effect is identical to that of the F and E data descriptors.  The format of the data descriptor is:

G<w>.<s>

where <w> specifies the number of digits in the external representation of the number, including the decimal point, and <s> specifies the number of significant digits rather than the number decimal places.  Using the G data descriptor, an output value will be printed in a F style if that is possible in a field width of <w> - 4.  Otherwise, it is printed in the E style.  E style numbers are right-justified, and F style numbers are justified to the fifth from the right space of the field, leaving four spaces to the right of the number. 

On INPUT, the operation is identical to the F and E data descriptors.

On OUTPUT, the rightmost four spaces in the field of width <w> are reserved for a decimal point shift of the form E+xx or E-xx (each x is a single digit).  If the number rounded to <s> significant digits will fit in the remaining <w> - 4 leftmost spaces in the field, then the rightmost four spaces are left blank.  On the other hand, if the number won't fit into <w> - 4 spaces, then it is written with a decimal point shift factor.  In this case the number itself is rounded to <s> significant digits and is written in the form b<x>.<y> or -<x>.<y>, where <x> is a single digit and <y> is a string of <s> - 1 digits.

Internal                                 G11.4                                 External (b = blank)

-1.7526843

b-1.753bbbb

-175268.43

b-1.753E+05

3.1416

bb3.142bbbb

0.000031416

bb3.142E-05

Double Precision Data Descriptor

Floating point (REAL) values may be stored internally in variables defined as DOUBLE PRECISION in order to increase the accuracy of computations.  The input and output formatting and translation are identical to the E data descriptor.  The only difference is that the variable named in the data list must be either implicitly or explicitly declared to be of the type DOUBLE PRECISION.  The format of the data descriptor is:

D<w>.<d>

where <w> specifies the number of digits in the external representation of the number, including the decimal point, and <d> specifies the number of digits which may be present to the right of the decimal point..

LOGICAL Data Descriptor

The LOGICAL data descriptor describes a single alphanumeric character (T or F) which is represented internally as the corresponding value of either .TRUE. or .FALSE.  The format of the data descriptor is:

L<w>

where <w> specifies the number of character positions which contain the data on the external media.

On INPUT, the corresponding LOGICAL variable in the input list is given the value .TRUE. if the first nonblank character in the data field is a T.  If the first nonblank character in the field is F, the variable is given the value .FALSE.

External (b = blank)                               L6                                     Internal

bbTbbb

.TRUE.

TURFbb

.TRUE.

bbbbbF

.FALSE.

Fbbbbb

.FALSE.

bTRUEb

.TRUE.

FALSEb

.FALSE.

On OUTPUT, the letter T or F is right-justified with blank fill to the left if the value of the corresponding memory cell in the output list is .TRUE. or .FALSE., respectively.

Internal                                 L6                                 External (b = blank)

.TRUE.

bbbbbT

.FALSE.

bbbbbF

Alphanumeric (Character) Data Descriptor

The Alphanumeric data descriptor describes a string of alphanumeric characters.  It is safest to store characters in INTEGER memory cells, but it is legal to use the A data descriptor in conjunction with variables of any type.  The format of the data descriptor is:

A<w>

where <w> specifies the number of character positions which contain the data on the external media.

On INPUT, the number of characters specified by <w> in the data field are placed into the corresponding variable of the variable list.  All characters in the field, blanks and single quotes included, are part of the character string placed into the variable.  If the variable isn't large enough to hold all <w> characters, then only the rightmost <w> characters in the data field are placed in the variable.  If the variable is large enough to hold more than <w> characters, then the <w> characters are placed into the leftmost part of the variable's storage and blank characters are put into the remainder of the storage.  Under MVT and MVS on S/370 hardware (as applies under Hercules emulation), the number of characters that may be stored in a single INTEGER variable is four, which is equivalent to the size of one fullword.

External

Data Descriptor

Internal (b = blank)

ABCD A4 ABCD
ABCDEF A6 CDEF
AB A2 ABbb

On OUTPUT, the characters stored internally in the variable named in the variable list are written in the <w> space output field.  If the variable has more than <w> characters, then only the leftmost <w> characters in the variable are written in the output field.  If the variable has fewer than <w> characters, then its characters are right-justified in the output field with blank filling to the left.

Internal

Data Descriptor

External (b = blank)

ABCD A4 ABCD
ABCD A2 AB
ABCD A6 bbABCD

Spacing Data Descriptor

The Spacing data descriptor designates a number of positions to be skipped when processing either an input or output record.   The format of the data descriptor is:

X<w>

where <w> specifies the number of character positions to be skipped.

On INPUT, processing of the external record will be skipped for <w> character positions, and then processing of the record will commence using the next data descriptor in the FORMAT statement.

On OUTPUT, <w> blanks will be transferred to the output record, achieving the same result as though a literal of <w> blanks had been specified in the FORMAT statement.

Repeat Specifications and Groups

Formatted records, whether input data or printed/punched output, are frequently a very regular structure.  In FORTRAN, when a consecutive series of data items are identical, it is simpler to use a repetition factor to specify the data descriptors rather than coding each individual descriptor.  Therefore, these two FORMAT statements will achieve the same result:

1000 FORMAT (F8.0, F8.0, F8.0, F8.0, F8.0, F8.0, F8.0)

2000 FORMAT (7F8.0)

The repeat specification is an unsigned integer constant which appears in front of a data descriptor.

If the same pattern for data descriptors is repeated several times, then that pattern may be grouped by parentheses and a repeat specification placed in front of the group.  Therefore, these two FORMAT statements will achieve the same result:

1000 FORMAT (' ', F11.4, ' AND', I10, 20X, F11.4, ' AND', I10, 20X, I15)

2000 FORMAT (' ',2(F11.4, ' AND', I10, 20X), I15)

Any list of data descriptors can become a group, and groups may be nested two deep.

The following sections regarding the FORMAT statement apply only to FORMAT statements associated with WRITE statements.

Literal Constants

Literal constants contained in the FORMAT statement are transferred to the output record without modification.  Literal constants are specified by enclosing the literal value inside of single quotations.  If the constant contains a single quotation, such as O'Brian, the quotation mark that is part of the constant must be specified by coding two consecutive single quotation characters. 

1000 FORMAT (' HELLO WORLD!')

2000 FORMAT ('MY NAME IS BILL O''BRIAN.')

Carriage Control

The first character of each record written to a printer is interpreted by the operating system as a carriage control character.  If you forget to include a specific character for this purpose, the operating system will gladly appropriate the first data character of each printed line for this purpose.  Not only will the vertical spacing of your output be very strange, you will lose the first character of your output data.  The acceptable values to be used for carriage control are:

Character Results in Positioning
' ' (blank) move down the page one line before printing
'0' (numeric zero) move down the page two lines before printing
'1' (numeric one) move to the top of the next page before printing
'+' (plus sign) print on the current line (overprint last line printed)

Next Record Indicator

In general, a single FORMAT statement describes the fields occurring on a single data record.  However, it is possible for a single FORMAT statement to describe more than one record by using a forward slash (/) to indicate the end of the current data record and the beginning of the next data record.

Most frequently, this technique will be used in the description of printed output.  Here is a FORMAT statement that produces two lines of heading information on a report:

1000 FORMAT ('1', 'PLAYER1  PLAYER2'/ ' ', '----------------')

Notice that each line has its own carriage control character.

Two consecutive slashes imply a blank record in between.  The printer will skip a line because the implied blank line includes a blank character for carriage control.  The input device will skip a data record when given a // specification.  A slash at the end of a FORMAT always implies a blank printed line (for output) or a skipped record (for input).

 

GO TO Statement

FORTRAN is not a language a programmer can use and avoid the use of GO TO statements.  The only way to deal with it is to design the most structured program possible and carefully, judiciously use the statement.

The target of the GO TO statement is a label defined by placing a unique number between 1 and 99999 in the label field of another statement.  Then the GO TO is simply coded as:

GO TO <n>

where <n> is the number on the statement to which control is to be transferred.

A label may appear on any executable FORTRAN statement.  FORTRAN also supplies the CONTINUE statement, which is an executable statement which does nothing, making it an excellent statement to use as the logical and obvious target of GO TO statements.

Computed GO TO

There is a second format of the GO TO statement, referred to as the computed GO TO, which has the format:

GO TO (<n1>, <n2>, ... <nn>) <i>

where <n1>, <n2>, <nn> are statement labels and <i> is an integer variable name.  Upon execution, the value of <i> is used as an ordinal index to select the statement label to which control is passed.  If the value of the variable <i> is 1, control passes to the first statement label, <n1>.  If the value is 4, control is passed to the fourth statement label, <n4>.  If the value of <i> is zero or greater than the number of statement labels, control is passed to the next sequential FORTRAN statement.  Any statement label contained in the list may occur more than one time.

 

CONTINUE Statement

The CONTINUE statement is classified as an executable statement, however, it functions solely as a place marker as it does nothing but allow control to pass to the next executable statement sequentially.  It is most often used as the termination statement of a DO loop, but might be very well used as the termination point of any change of control, such as the target of a GO TO statement.  The format is:

<n> CONTINUE

where <n> is a unique statement label (number) in the range of 1 to 99999.

 

IF Statement

The IF statement evaluates one or more expressions and, if the expression evaluates to a value of .TRUE., the executable statement on the IF statement is executed.  If the expression evaluates to a value of .FALSE., execution of the program proceeds with the next statement in sequence.  The format of the IF statement is:

IF (<e1> <rel> <e2>) <s> 

where <e1> and <e2> are either numeric or logical expressions, <rel> is a relationship operator, and <s> is an executable statement to be executed if the relationship evaluates to .TRUE.

The acceptable relationship operators are:

FORTRAN Operator Relation
.LT. less than
.LE. less than or equal to
.EQ. equal to
.NE. not equal to 
.GT. greater than
.GE. greater than or equal to

Compound evaluations may be made by using .AND. or .OR. to combine relationship evaluations.  The outcome of an evaluation may be negated by using .NOT.  If you need to code a complex IF statement, use parenthesis to ensure the compiler will evaluate the expressions in the order you intend.

Some examples:

IF (CNT .GT. 0 .AND. AVG LE 100.0) GO TO 1001

IF (CNT .GT. 0 .OR. (AVG .GT. 100.0 .AND. AVG .LT. 200.0)) GO TO 501

 

STOP Statement

The STOP statement terminates execution of the FORTRAN program.  The format of the STOP statement is:

STOP <n>

where <n> is an optional integer which is passed to the operating system as a Return Code.

Good structured programming design dictates that a program will have only one entry and one exit, which would mean that there would only be a single STOP statement coded in each program.  However, since FORTRAN IV does not include modern structured constructs, it may be necessary to have multiple STOP statements in a FORTRAN IV program.

 

END Statement

The END statement signals to the compiler that the last statement of a source program has been read.  The format of the END statement is:

END

There may be several FORTRAN programs stacked in an input stream to be compiled together as a single run unit.  Each of the FORTRAN programs must be terminated by an END statement.  An illustration of this would be a FORTRAN main program which includes one or more FORTRAN subprograms and/or FORTRAN function subprograms that will be compiled in a single pass and then passed to the link-editor to be prepared for immediate execution in a single jobstream.

 

DATA Statement

The DATA statement is used to initialize variables at compile time, rather than to allow the compiler to initialize variables with the default values based upon data type.  The format of the DATA statement is:

DATA <v>  / <d> /

where <v> is a list of one or more variable names, separated by commas, and <d> is a list of data values used to initialize the corresponding variable from the list of variable names.

A single DATA statement can initialize more than one type of variable.

Examples:

REAL AVG, TOT
INTEGER CNT
LOGICAL YES, NO
DATA AVG, TOT, CNT, YES, NO / 0.0, 0,0, 0, .TRUE., .FALSE. /

It is also permissible to state the data adjacent to the variable and still make multiple assignments:

DATA A / 10.0 /, B / 20.0 /, C / 30.0 /

A DATA statement used to assign the same data value to a group of variables may take advantage of a repetition factor:

DATA R, S, T, U, V / 5*0.0 /

When assigning the value of an array variable, specifying the name of the array is implied to be the equivalent of specifying each element of the array:

INTEGER AMT(12)
DATA AMT / 12*0.0 /

 

DIMENSION Statement and Arrays

The DIMENSION statement is used to tell the compiler how many elements will be contained in an array and must appear before any executable statement in the program.  The format is:

DIMENSION <v>(<s>) |, <v>(<s>), ... <v>(<s>)

where <v> is the name of the array variable and <s> is the maximum number of elements that will be stored in the array.  FORTRAN allows for the use of arrays with up to three dimensions, so the entry for <s> may specify from one to three values, separated by commas.  

Array variables are accessed using the name of the array variable followed by a subscript enclosed in parentheses:

TOT(1) = TOT(1) + BAL

     or

IX = 1
TOT(IX) = TOT(IX) + BAL

 

NOTE:  If an array variable is explicitly typed with a REAL, INTEGER, DOUBLE PRECISION, COMPLEX, or LOGICAL statement, it is possible to specify the characteristics of the array on the type statement.  It is then not necessary to include a DIMENSION statement defining the array.  For example, to declare an array of type DOUBLE PRECISION to hold 50 values:

DOUBLE PRECISION AVG(50)

 

Input and Output of Array Data

The most straightforward method for input and output of array data is to list each array element name in the READ or WRITE statement:

         READ(5,1001) B(1), B(2), B(3), B(4), B(5)
1001 FORMAT (5F8.2)

Obviously, for large arrays this is an impractical way to code.  Instead, the FORTRAN compiler understands the inclusion of an array name by itself on a READ or WRITE statement to mean all values are to be input or output from the entire array.  Therefore, the following READ statement is equivalent to the one shown above:

         READ (5,1001), B
1001 FORMAT (5F8.2)

It is also possible to explicitly code input/output of array data using a DO loop:

         DO 20 I=1,4
           WRITE (6,1002) B(I)
1002 FORMAT (F8.2)
    20 CONTINUE

And it is also permissible to code an implied DO loop on the READ or WRITE statement:

        WRITE (6,1002) (B(I),I=1,4)
1002 FORMAT (F8.2)

 

DO Statement

The DO statement is the only iterative control statement provided in FORTRAN IV.  The format of the statement is:

DO <s> <i> = <m1>, <m2> |, <m3>

where <s> is the statement label number of the last statement which is included in the loop, <i> is an integer variable which controls the iteration of the loop.  <m1>, <m2> and <m3> are either unsigned integer constants or integer variables containing a positive value.  <m1> is the initial value assigned to <i> prior to the first iteration of the loop.  <m2> is the value to which <i> is compared at the conclusion of each iteration of the loop, and, when reached the loop is terminated.  <m3> is an optional value by which <i> is incremented at the conclusion of each iteration of the loop.  If <m3> is omitted, the value of 1 is assumed.

The statement label number <s> must refer to an executable statement.  The CONTINUE statement is frequently used as the termination statement of a DO loop.  When the loop has concluded, by the value of the control variable <i> reaching the limiting value of <m2>, control is passed to the statement following the statement referred to by <s>.

 

Subprograms

As in other high-level languages, the functionality of a problem program may be divided into modular units.  The subordinate units then become subprograms which are called in the proper sequence by the main program.  The MVT FORTRAN compiler can compile any number of subprograms included in the input jobstream along with the main program.  Alternatively, subprograms can be separately compiled and link edited into a library from which the subprograms are either bound with the main program during linkage editing or loaded dynamically during run time.

There are two types of subprograms in FORTRAN, subroutine subprograms and function subprograms.  The difference between the two are the way in which the subprogram is called from the main program and the number of arguments, or variables, that are returned to the main program.  Subroutine subprograms are invoked by the CALL statement and may return multiple values.  Function subprograms are invoked as though they were a variable and return a single value.

The first statement in a subroutine subprogram consists of the SUBROUTINE statement.  The format is:

SUBROUTINE <name>(<a1>, <a2>, ... <an>)

where <name> is the name of the subprogram and <a1>, <a2>, <an> are any number of optional argument variables.  The <name> of the subprogram must follow the rules for variable name formation.

Variable values are passed by reference, and the data type and size of the variables must agree between the SUBROUTINE statement and the CALL statement used to invoke the subprogram.  For example:

C        MAIN PROGRAM
           INTEGER A,B,C
           REAL AVG
           CALL SAVG(A,B,C,AVG)
           ...
           END

C         SUBPROGRAM
            SUBROUTINE SAVG(I1,I2,I3,R1)
            INTEGER I1,I2,I3
            REAL R1
            R1 = (I1+I2+I3)/3.0
            RETURN
            END

It is irrelevant that the names of the variables are not identical in the coding of the CALL statement in the main program and the SUBROUTINE statement in the subprogram.  What is important is that there is agreement in the type of the variables passed - A and I1 are INTEGERS, B and I2, etc.

Function Subprograms

The first statement in a function subprogram consists of the FUNCTION statement.  The format is:

FUNCTION <name>(<a1>, <a2>, ... <an>)

where <name> is the name of the subprogram and <a1>, <a2>, <an> are any number of optional argument variables.  The <name> of the subprogram must follow the rules for variable name formation.  Unlike subroutine subprograms, the <name> must correspond to the type of data to be returned to the calling program, and so must be either implicitly or explicitly typed.  If explicit typing is used, the type declaration statement must appear in the calling program.

The example subroutine subprogram above can be easily converted into a function subprogram:

C        MAIN PROGRAM
           INTEGER A,B,C
           REAL AVG,FAVG
           AVG = SAVG(A,B,C)
           ...
           END

C         SUBPROGRAM
            FUNCTION FAVG(I1,I2,I3)
            INTEGER I1,I2,I3
            FAVG = (I1+I2+I3)/3.0
            RETURN
            END

 

RETURN Statement

Both subroutine subprograms and function subprograms return control to the calling program with the RETURN statement.  The format of the statement is:

RETURN

There can be any number of RETURN statements in a given subprogram, although proper structured design dictates a single exit point from any routine.

 

Statement Functions

Statement functions are similar to function subprograms, except that they must be defined on a single line.  The function definition must appear in the program in which it is called, and must occur before any executable statements in the program.  For example:

C        MAIN PROGRAM
           REAL A,B,C,AVG
           AVG3(V1,V2,V3) = (V1+V2+V3)/3.0
           ...
           AVG = AVG3(A,B,C)
           ...
           END

 


Return to Site Home Page 


This page was last updated on January 29, 2015.