Basics
A Fortran program consists of a main program augmented by program units
(subroutines, functions, and modules). The main program is a program
unit itself. Each program unit has a declaration section, where
variables, etc. are specified, followed by an execution section with
executable (at run-time) statements. Program units end with an
end
statement.
A simple "hello world" program looks like this:
program hello_world
write (*,*) 'Hello World!'
end program hello_world
Here we are using the write
command to print to the screen. The first
argument of this command specifies where to print and what format to
use. For this simple statement we use (*,*)
. The first star specifies
printing to standard out. The format (the second star) is left to
Fortran. More on I/O follows in a later section.
Alphanumeric characters (letters, underscore, and numerals) are used in Fortran tokens (keywords) and variables. Special characters may appear in a variety of contexts. Blanks may be used freely to improve layout:
/ ( ) , = => : :: ; %
Blanks are ignored except when they are in literal strings. This is strictly true for the free format. This means that blanks may be inserted into keywords. Also variable names may have blanks; the variable "sum" may be written as "s u m" . Neither practice is recommended. Fixed format is stricter and does not allow blanks in keywords or variable names.
There is no distinction between upper and lower case, except in literal strings. Variables start with a character and can include numerals and the underscore. In Fortran 90 and 95, the length of a variable (and function and subroutine) names is at most 31 characters.
Fortran supports 5 basic types of variables: real
,
integer
, logical
(Boolean), complex
(complex variables), and character
(strings). Literal
constants of these types are:
Example | Variable Type |
---|---|
5. |
a real literal constant |
5 |
an integer literal constant |
.true., .false. |
the two logical literal constants |
(1.0,2.0) |
a complex literal constant |
'hello' |
a string literal constant |