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.
Suppose we have the following main program and subroutine:
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: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
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!
There are some problems in the above argument associations. Let us examine all five actual/formal arguments: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
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.