I,J,...,N is an INTEGER;
any other starting character of its name makes it a REAL. Until
it is assigned to, an implicitly declared variable has value zero.
You can change this implicit declaration scheme by a statements such as
IMPLICIT INTEGER(H-K)
IMPLICIT LOGICAL(P-R)
Implicit declaration is very dangerous, since any typo will result in a
new and unintended variable, with value zero. Many compilers recognise the
IMPLICIT NONE statement (not part of Fortran77), which forces
you to declare every variable explicitly, if they don't, there is usually
a flag to make the compiler complain about undeclared variables.
DATATYPE LIST-OF-NAMESwhere
DATATYPE is any of the simple
datatypes, and the list is a comma-separated series of variables.
REAL X(20)The alternative is to declare the type and the size of the array separately. If the type has been declared explicitly or implicitly, the size is specified as
DIMENSION X(20)
DIMENSION Y(5,3),Z(50,-1:1)
et cetera.
DATA
statement. Example: a single variable is initialised with
DATA X/1.0/Two variables can be initialised either as
DATA X/1.0/,Y/2.5/or
DATA X,Y/1.0,2.5/Data statements can be used to initialise arrays in various ways.
Variables initialised in a DATA statement do not keep their
initial value forever: once that variable appears in an assignment or READ
statement, its initial value is gone. Also, the value in the DATA statement
is only assigned at compile time, that is, it is present at the start of
the program. If the statement appears in a subprogram and the variable is
assigned to the first time the subprogram is executed, the initial value
is no longer in place the second time the subprogram is executed.
PARAMETER (HALFPI = 1.57)This makes your code more intelligible, and if ever the number has to be changed, you need only change it in one instance.
You can introduce several parameters in one statement:
PARAMETER (N = 3, M = 5)and you can define parameters in terms of other parameters:
PARAMETER (PI = 3.14159)
PARAMETER (HALFPI = PI/2)
You can even use functions such as MAX in the parameter statement,
but you can not involve ordinary variable or subprogram arguments.
The rule is that the value has to be computable by the compiler,
before the program is ever run.
The variable in a parameter statement looks a lot like an ordinary variable, eg you have to declare its type, and the statement itself looks like an assignment. Do not let that fool you. Such an identifier is really a named constant. You can not use it in an ordinary assignment.
The name parameter is somewhat unfortunate, since this term is often also used as synonym of `argument'.
COMMON block. Example:
COMMON /PROGVARS/MAXSIZE,MINSIZEIt is necessary to declare the type of variables in common, and inside one common block all variables have the same type. To share variables of more than one type, you need more than one common block. Example:
COMMON /IVARS/IMAX,IMIN
COMMON /RVARS/RMAX,RMIN
These are named common blocks; there can be exactly one blank
common block, which is simply a list of variables.
Data in blank common can not be initialised with a DATA
statement; data in named common can only be initialised in a completely
unexecutable program unit: a BLOCK DATA unit. This type of unit contains
only non-executable statements.
In addition to the function definition, you need to declare the type of the function name and its arguments in the function definition.
VAR = valuewhere
value can be another variable, a constant
value, an array element, or
an expression. The interpretation
of this statement is: evaluate the right-hand side to find its value, then
assign that to the variable on the left-hand side.
And assignment statement may look like a mathematical equility, but do not let that deceive you. The line
X = X+1cannot possibly be mathematically true, but as an assignment it means "find the current value of
X, add one to it, and assign that value
to X".
There are other ways of changing the value of a variable, namely by passing it as a subroutine or function argument, through initialisation in a data statement, or by reading in the value.
CALL SPROGfor a routine without arguments, and
CALL SPROG(ARG1,ARG2,...)for routines with arguments.
CONTINUE statement
CONTINUEhas no effect. Its main purpose is to serve as target for jumps, or as the final statement of
DO
loops.
DO
loop), and the FORMAT
statement. Labels are placed in the first 5
columns of a line.