7.4. FORTRAN

FORTRAN, one of the widely used high level programming language which has been used by scientists and engineers from the early days of developing computer languages. Simplicity and versatility of this programming language made it popular for numerical computing and scientific applications. The name FORTRAN stands for FORmula TRANslation, reflecting its development for efficient handling of mathematical formulas and equations.

FORTRAN has been revised many times. Although the first standardized FORTRAN compiler ANSI (American National Standards Institute) was introduced in 1966, it has been revised many times. FORTRAN 77, targeted for release in 1977, appeared in 1978. Most compilers follow ANSI standards, although extra features are added. To develop a portable program, programmers must be familiar with the standard compiler as well as additional extensions to their compiler.

7.4.1. FORTRAN COMPILE LINK AND RUN

7.4.1.1. FORTRAN PROGRAMS

FORTRAN programs are entered using an editor. They should be typed in upper case and include the PROGRAM statement. Do not use any GOTO statements in your code.

The first few lines of the program must be some comments to identify the program. These should include information such as your name, student number, class, professor, assignment number and due date.

example:

C NAME: CAROLYN STEEN

C STUDENT NUMBER: 19299999

C CLASS: COMPUTER SCIENCE 203

C PROF: DR. H. RAAFAT

C ASSIGNMENT: #1

C DUE: SEPTEMBER 18, 1992

7.4.1.2. COMPILE

Once you have created your program using the editor it must be compiled and linked before it can be executed. The file which contains your program should have a .FOR extension. To compile the program, type:

$ FORTRAN/options filename.FOR

In the "filename" you do not have to include the .FOR extension. The options you desire are placed after the FORTRAN command and separated by slashes. Some possible options are:

/LIST create a listing file (filename.LIS) which contains the source code and error messages which resulted from the compile

/NOOBJECT do not create an object file (filename.OBJ). This is used if you want to check if there are errors during the compile without intending to run the program.

/STANDARD=([NO]SYNTAX,[NO]SOURCE_FORM)

gives warning messages whenever the source code is not ansi-standard FORTRAN-77. "Syntax" refers to the FORTRAN statements and "source_form" refers to the aligning of the program in the proper FORTRAN columns.

The default is :

STANDARD=(SYNTAX,NOSOURCE_FORM).

/CROSS_REFERENCE

produces a cross reference listing as part of the symbol table

You will be expected to compile your programs using the STANDARD, LIST and CROSS_REFERENCE options. If you were to compile your program to hand in (i.e. requiring a listing and with the standards and cross_reference options) you would type:

$ FORTRAN/STANDARD/LIST/CROSS_REFERENCE filename

The compiling of your program results in an object file. This file has the same name as your original code with a .OBJ extension replacing the .FOR extension. This file contains the commands your program is to execute in a form that the computer can understand.

7.4.1.3. LINK

Once you have all the syntax errors out of your program (no errors on the compile step), you can link your .OBJ file to create a .EXE, or an executable file. In this file any reference to code not contained in your source program file is satisfied so that the program is ready to execute. To link the program type:

$ LINK filename.OBJ

You may omit the .OBJ extension. This creates an executable file called "filename.EXE".

7.4.1.4. RUN

To run the program you simply type:

$ RUN filename.EXE

You may leave off the .EXE extension.

7.4.1.5. FORTRAN COLUMNS

FORTRAN programs were originally entered onto computer cards using the card columns to structure the specific FORTRAN fields. The standard source forms for FORTRAN-77 requires that these fields be adhered to. The columns are:

column 1-5 statement numbers

column 6 continuation

columns 7-72 FORTRAN statement body

columns 73-80 identification

If you do not use the /STANDARD=(SOURCE_FORM) option on the compile, it is not necessary to follow these columns strictly.

7.4.1.6. FORTRAN FUNCTIONS

Along with the FORTRAN compiler there are a large number of functions available for your use. A description of these functions and how to use them can be found in HELP under the Topic FORTRAN and subtopics Built_in_Functions and Intrinsic_Functions. A list of some of the functions available follows:

Function Name Purpose

ABS Absolute Value

ACOS Arc Cosine in radians

ACOSD Arc Cosine in degrees

AIMAG Imaginary Part of a Complex Number

AMAX0 Greatest value in a list of real numbers

AMIN0 Smallest value in a list of real numbers

ASIN Arc Sine in radians

ASIND Arc Sine in degrees

ATAN Arc Tan in radians

ATAND Arc Tan in degrees

CHAR The character value of an ascii code

COS Cosine in radians

COSD Cosine in degrees

COSH Hyperbolic Cosine

DATE Current Date

ERRSNS Information about the last FORTRAN error

EXIT terminates the program

EXP Exponentiation

IABS Absolute Value

ICHAR Ascii value of the character

IDATE Current day, month, and year

INDEX Search string for a substring

INT Integer part of a real number

LEN Length of a string

LOG Natural Logarithm

LOG10 Common Logarithm (Base 10)

MAX Largest value in a list of integers

MIN Smallest value in a list of integers

MOD Gives remainder of division

RAN Random Number Generator

REAL Converts to Real value

SIN Sine in radians

SIND Sine in degrees

SINH Hyperbolic Sine

SQRT Square Root

TAN Tangent in radians

TAND Tangent in degrees

TANH Hyperbolic Tangent

TIME The current time

7.4.2. FORTRAN INPUT AND OUTPUT

7.4.2.1. TERMINAL INPUT AND OUTPUT

FORTRAN programs can read from the keyboard and write to terminal screen with the statements:

READ *, input-list

PRINT *, output-list

where:

input-list one or more variables separated by commas

output-list one or more expressions separated by commas

The expressions can be constant, variable, formula or a character string enclosed by single quotes.

Example:

PRINT * , 'Please enter a number'

READ * , number

PRINT *, 'The number is ', number

It is a good practise to print out a statement to direct the user to input data.

7.4.2.2. ASSIGNING FILE INPUT AND OUTPUT

FORTRAN programs can read and write to files with the statements:

READ (15,1000) abc

WRITE (20,1010) def

This will read input from a file called 'FOR015.DAT' in your account and will write to a file called 'FOR020.DAT'. The default filenames are created with 'FOR' followed by the three digit device number from the read or write statement, and then the extension '.DAT'.

If you are running a program a number of times with different sets of data it is inconvenient to have to rename the files or modify the program read and write statements to use different device numbers. It would be more convenient if you could override the default input and output filenames to read from and write to filenames of your choice. This can be done using the 'ASSIGN' or 'DEFINE' commands.

The format for the two commands is:

$ ASSIGN physical_device logical_device

$ DEFINE logical_device physical_device

where: physical_device is the name of the file which physically exists on the disk.

logical_device is the name of the file with which you want to associate the physical filename

These commands are equivalent, and are used to set up a substitution. Whenever the logical filename is accessed the physical filename is substituted in place of it. In this way the statements:

$ ASSIGN diskfile FOR###

$ DEFINE FOR### diskfile

where ### is the three digit device number, could be used to access a file of another name by a FORTRAN program. The diskfile which you make for input, or that is created by output will again have an extension of '.DAT'.

Using these same commands it is also possible to have terminal input or output which is intended to go to the screen be placed in a file, or alternately to have input or output which is intended to use a file to use the terminal. This can be done by using the keywords SYS$INPUT to indicate input from the terminal and SYS$OUTPUT to indicate output to the terminal. For example, the following commands could be used to read and write to a file when the program is set up to use the terminal for input and output:

$ ASSIGN/USER filename SYS$OUTPUT

$ ASSIGN/USER filename SYS$INPUT

To run a program which is intended to read and write to files and have it accept terminal input and send output to the terminal, the following commands could be used:

$ ASSIGN/USER SYS$OUTPUT filename

$ ASSIGN/USER SYS$INPUT filename

Since the ASSIGN and DEFINE commands are in effect until you either deassign them or do another assign which changes the original value, when dealing with terminal input and output the /USER qualifier is used. This makes the command take effect for only the next entered command. After this it will be discarded.

If you have made an assignment and wish to discard it, this may be done using the DEASSIGN command. The format of this command is:

$ DEASSIGN logical_device

where the logical_device is the same as the logical device in the assignment statement.

7.4.3. FORTRAN LOOP STRUCTURE

A loop is a block of statements that is needed to be executed more than once. The loop must be controlled so that it won't be executed infinitely.

7.4.3.1. DOWHILE LOOP

For DOWHILE loop, the logical expression is used to control how many times the block of statements will be executed.

Format:

DOWHILE (logical-expression)

.

.

.

ENDDO

Note: DOWHILE is not standard FORTRAN statement!!

Example:

PRINT *, 'Please enter a number'

READ *, num

DOWHILE (num .ne. 0)

PRINT *, 'The number is', num

PRINT *, 'Please enter a number'

READ *, num

ENDDO

7.4.3.2. IF AND GOTO LOOP

The IF and GOTO loop is similar to the DOWHILE loop. The logical expression is also used to control the repetition of the loop.

Format:

# .

IF (logical-expression) THEN

.

.

GOTO #

ENDIF

Note: # represents statement number

Example:

100 PRINT *, 'Please enter a number'

READ *, num

IF (num .ne. 0) then

PRINT *, 'The number is', num

GOTO 100

ENDIF

Even though the IF and GOTO loop is easier to used, DOWHILE loop is recommended. Also, using GOTO statement is considered a bad programming structure. DON'T use GOTO statement unless you have no other choice.

7.4.3.3. DO AND CONTINUE LOOP

DO and CONTINUE loop uses counter to control how many times the loop will be executed. Actually, the number of times is determined before the loop is executed.

Format:

DO #, control-variable = initial-value, limit, step-size

.

.

.

# CONTINUE

Note: # represents statement number

Example:

DO 200, num = 1 ,10, 2

PRINT *, num

200 CONTINUE

7.4.4. FORTRAN SUBROUTINES

When writing a segment of code that has a very particular purpose it can be stored in a subroutine and then simply called by the main program to execute this function. Subroutines are also used where a segment of code is required more than once; it may be written once and called many times.

The subroutine may need some information or parameters to be given to it for use in the routine. This is referred to as passing parameters to a subroutine.

Format:

SUBROUTINE name (parameter1,parameter2,....)

code to be executed

.....

RETURN

END

A call to a subroutine has the format:

CALL name (parm1,parm2,...)

The parameters passed by the main must be the actual variable which either contains a value needed by the subroutine or is to contain a value produced by the subroutine. The parameters in the subroutine can be any dummy variable names that will be matched in order with the actual parameter names. These dummy variable names will be used in the subroutine to manipulate and reference the actual parameters -- pass by reference.