We have met INTENT(IN) in
function's discussion. It
indicates that an argument will receives some input from outside of the
function and its value will not, actually cannot, be changed within the
function. Since a subroutine cannot return a value through its name, it
must return the computation results, if any, through its argument.
Therefore, we have three cases to consider:
If an argument only receives value from outside of the subroutine,
it still has its intent like INTENT(IN). This is the
simplest case.
An argument does not have to receive anything from outside of the
subroutine. It can be used to pass a computation result back to
the outside world. In this case, its intent becomes
INTENT(OUT). In a subroutine, an argument declared with
INTENT(OUT) is supposed to hold a computation result
so that its value can be passed "out".
Finally, an argument can receive a value, use it for computation,
and hold a result so that it can be passed back to the
outside world. In this case, its intent is INTENT(INOUT).
An argument must be declared with INTENT(IN),
INTENT(OUT) or INTENT(INOUT).
Examples
Here are some examples:
The following subroutine Means() has six arguments.
Arguments a, b and c are declared with
INTENT(IN) and therefore can only take values from outside
world and cannot be changed. Arguments Am, Gm and
Hm are declared with INTENT(OUT), indicating that
their values will be computed and passed to the outside world.
More precisely, in subroutine Means(), some values must be
stored into these three arguments so that they can be passed out.
Note that an argument declared with INTENT(OUT) does not
have to receive any value from outside of the subroutine.
SUBROUTINE Means(a, b, c, Am, Gm, Hm)
IMPLICIT NONE
REAL, INTENT(IN) :: a, b, c
REAL, INTENT(OUT) :: Am, Gm, Hm
..........
END SUBROUTINE Means
The following subroutine Swap() has its both arguments
declared with INTENT(INOUT). That means, a and
b will receive some values, after some processing a new
set of values will replace the given one so that they can be
passed back.
SUBROUTINE Swap(a, b)
IMPLICIT NONE
INTEGER, INTENT(INOUT) :: a, b
..........
END SUBROUTINE Swap