More about Argument Association

INTENT(IN)

We have discussed the meaning of INTENT(IN) earlier in functions. Simply speaking, a formal argument declared with INTENT(IN) means it only receives a value from its corresponding actual argument and its value will not be changed in this function or subroutine.

INTENT(OUT)

A formal argument declared with INTENT(OUT) serves the opposite purpose. It means that formal argument does not have to receive any value from its corresponding actual argument. Instead, at the end of the subroutine's execution, the most recent value of that formal argument will be passed back to its corresponding actual argument.

From the caller's point of view, an actual argument whose corresponding formal argument is declared with INTENT(OUT) does not have to have any valid value because it will not be used in the subroutine. Instead, this actual argument expects a value passed back from the called subroutine.

INTENT(INOUT)

A formal argument declared with INTENT(INOUT) expects a valid value from the caller and sends a value back to the caller. Therefore, the caller must supply a valid value and the subroutine must generate a valid value so that it can be passed back.

Suppose we have the following main program and subroutine:

PROGRAM  TestExample          SUBROUTINE  Sub(u, v, w)
   IMPLICIT  NONE                IMPLICIT  NONE
   INTEGER :: a, b, c = 5        INTEGER, INTENT(IN)    :: u
   a = 1                         INTEGER, INTENT(INOUT) :: v
   b = 2                         INTEGER, INTENT(OUT)   :: w
   CALL  Sub(a, b, c)            w = u + v
   .....                         v = v*v - u*u
END PROGRAM  TestExample      END SUBROUTINE  Sub
Subroutine Sub() has three formal arguments. u is declared with INTENT(IN) and receives 1. v is declared with INTENT(INOUT) and receives 2. w is declared with INTENT(OUT) and its final value will be passed back to the main program replacing the value of c. The following diagram illustrate this relationship:

The Situation Is Not So Simple, Though

An actual argument could be a variable, a constant, or an expression. We have seen in function's discussion that one can pass a variable, a constant or an expression to a formal argument declared with INTENT(IN). The expression is first evaluated, its result is stored in a temporary location, and the value of that location is passed. Note that a constant is considered as an expression.

How about arguments declared with INTENT(OUT) and INTENT(INOUT)? That is simple. Please keep in mind that the corresponding actual argument of any formal argument declared with INTENT(OUT) or INTENT(INOUT) must be a variable!

PROGRAM  Errors                    SUBROUTINE  Sub(u,v,w,p,q)
   IMPLICIT  NONE                     IMPLICIT  NONE
   INTEGER :: a, b, c                 INTEGER, INTENT(OUT)   :: u
      ..........                      INTEGER, INTENT(INOUT) :: v
   CALL  Sub(1,a,b+c,(c),1+a)         INTEGER, INTENT(IN)    :: w
      ..........                      INTEGER, INTENT(OUT)   :: p
END PROGRAM  Errors                   INTEGER, INTENT(IN)    :: q
                                         ..........
                                   END SUBROUTINE  Sub
There are some problems in the above argument associations. Let us examine all five actual/formal arguments:

Are these rules rational? Yes, they are. If an actual argument is an expression, as mentioned earlier, it will be evaluated, its value is stored in a temporary location, and the value stored there is passed. Therefore, if its corresponding formal argument is declared with INTENT(OUT) or INTENT(INOUT), the result will be passed back to that temporary location which cannot be used by the caller. As a result, the actual argument must be a variable to receive the result.