Consider the following example Fortran 90 program:
MODULE Triangle_Operations IMPLICIT NONE CONTAINS FUNCTION Area(x,y,z) REAL :: Area ! function type REAL, INTENT( IN ) :: x, y, z REAL :: theta, height theta = ACOS((x**2+y**2-z**2)/(2.0*x*y)) height = x*SIN(theta); Area = 0.5*y*height END FUNCTION Area END MODULE Triangle_Operations PROGRAM Triangle IMPLICIT NONE REAL :: a, b, c, Area PRINT *, 'Welcome, please enter the& &lengths of the 3 sides.' READ *, a, b, c PRINT *, 'Triangle''s area: ', Area(a,b,c) END PROGRAM Triangle
The program highlights the following:
Executable statements do not have to start in or after column 7 as they do in FORTRAN 77.
MODULE Triangle_Operations
A program unit used to house procedures. (Similar to a C++ `class'.)
IMPLICIT NONE
Makes declaration of variables compulsory throughout the module. It applies globally within the MODULE.
Specifies that the rest of the MODULE consists of procedure definitions.
FUNCTION Area(x,y,z)
This declares the function name and the number and name of its dummy arguments.
REAL :: Area ! function type
FUNCTION s return a result in a variable which has the same name as the function (in this case Area). The type of the function result must be declared in either the header or in the declarations.
The ! initiates a comment, everything after this character on the same line is ignored by the compiler.
REAL, INTENT( IN ) :: x, y, z
The type of the dummy arguments must always be declared, they must be of the same type as the actual arguments.
The INTENT attribute says how the arguments are to be used:
REAL :: theta, height
The final REAL declaration statement declares local variables for use in the FUNCTION. Such variables cannot be accessed in the calling program, as they are out of scope, (not visible to the calling program).
theta = ACOS((x**2+y**2-z**2)/(2.0*x*y))
This statement assigns a value to theta and uses some mathematical operators:
The brackets (parenthesis) are used to group calculations (as on a calculator) and also to specify the argument to the intrinsic function reference ACOS.
Intrinsic functions are part of the Fortran 90 language and cover many areas, the simplest and most common are mathematical functions such as SIN and COS or MIN and MAX. Many are designed to act elementally on an array argument, in other words they will perform the same function to every element of an array at the same time.
height = x*SIN(theta); Area = 0.5*y*height
This highlights two statements on one line. The ; indicates that a new statement follows on the same line. Normally only one statement per line is allowed.
The function variable Area must be assigned a value or else the function will be in error.
PROGRAM Triangle
The PROGRAM statement is not strictly necessary but its inclusion is good practice. There may only be one per program.
IMPLICIT NONE
An IMPLICIT NONE statement turns off implicit typing making the declaration of variables compulsory.
REAL :: a, b, c, Area
Declaration of real valued objects. a, b and c are variables and Area is a function name. This function must be declared because its name contains a value.
PRINT *, 'Welcome, please enter the& ...
This PRINT statement writes the string in quotes to the standard output channel (the screen). The & at the end of the line tells compiler that the line continues and the & at the start of the text tells the compiler to insert one space and continue the previous line at the character following the &. If the & were at the start of the line, or if there were no & on the second line then the string would have a large gap in it due to the indentation being considered as part of the string.
READ *, a, b, c
This READ statement waits for three things to be input from the standard input (keyboard). The entities should be separated by a space. Digits will be accepted and interpreted as real numbers; things other than valid numbers will cause the program to crash.
PRINT *, 'Triangle''s area: ', Area(a,b,c)
This PRINT statement contains a function reference, Area, and the output of a string.
The '' is an escaped character, and is transformed, on output, to a single '. A single quote in would generate an error because the compiler would think it had encountered the end of the string and would flag the character s as an error. The whole string could be enclosed by "s which would allow a single ' to be used within the string without confusion. The same delimiter must be used at each end of the string.
The function call invokes the FUNCTION with the values of a, b and c being substituted for x, y and z.
END PROGRAM Triangle
An END PROGRAM statement terminates the main program.
Return to corresponding overview page