If the called subroutine has formal arguments, the CALL statement that calls that subroutine must have actual argument. This is the first form. However, if a subroutine does not have any argument, it can be called with the second form or the third form.CALL subroutine-name (arg1, arg2, ..., argn) CALL subroutine-name () CALL subroutine-name
The number and types of actual arguments in the CALL statement must match the number and types of the corresponding formal arguments
The main program calls subroutine Larger() with a CALL statement. Thus, the values of a and b are passed to u and v, respectively. In subroutine Larger(), after receiving values, it stores the larger one into w and then reaches END SUBROUTINE. Then, the value stored in w is passed back to its corresponding actual argument c and the control of execution goes back to the caller. In this case, it is the main program. Therefore, variable c receives the larger value of a and b.
PROGRAM Example1 SUBROUTINE Larger(u, v, w) IMPLICIT NONE IMPLICIT NONE INTEGER a, b, c INTEGER, INTENT(IN) :: u, v ......... INTEGER, INTENT(OUT) :: w CALL Larger(a, b, c) IF (u > v) THEN ......... w = u END PROGRAM Example1 ELSE w = v END IF END SUBROUTINE Larger
Since u and v receive values from and return values to the outside of Sort(), they are declared with INTENT(INOUT). Note that w is not declared with any INTENT since it is not a formal argument. In this subroutine, if u is greater than v, they are not in order and the three assignment statements exchange the values of u and v.
In the main program, the values of a and b are passed to u and v, respectively. After subroutine Sort() finishes its job, since u and v are declared with INTENT(INOUT), their results are passed back to a and b, respectively. As a result, the original values of a and b are destroyed by the returned values. For example, if a and b have values 5 and 3, respectively, then u and v receive 5 and 3. In subroutine Sort(), the values of u and v are exchanged and returned to a and b. Hence, after returning to the main program, the values of a and b are 3 and 5.
PROGRAM Example2 SUBROUTINE Sort(u, v) IMPLICIT NONE IMPLICIT NONE INTEGER a, b INTEGER, INTENT(INOUT) :: u, v ......... INTEGER :: w CALL Sort(a, b) IF (u > v) THEN ......... w = u END PROGRAM Example2 u = v v = w END IF END SUBROUTINE Sort
From this description, it is clear that p should be declared with INTENT(IN) since its value is unchanged. Argument q should be declared with INTENT(INOUT), since 1 is added to it or -1 is subtracted from it. To add a value to or subtract a value from it, q must have an existing value and should be passed into subroutine DoSomething(). Finally, r is declared with INTENT(OUT), since its value is not needed for computation.
For the main program, if the value read into a is 7, then the CALL will receive 1 for b and 1 for c. If the value read into a is -4, b and c should receive -1 and 2 from subroutine DoSomething(). If a receives a value of 2, since q is not changed in DoSomething(), b and c receive 0 (unchanged) and 3, respectively.
PROGRAM Example3 SUBROUTINE DoSomething(p, q, r) IMPLICIT NONE IMPLICIT NONE INTEGER :: a, b, c INTEGER, INTENT(IN) :: p .......... INTEGER, INTEGER(INOUT) :: q READ(*,*) a INTEGER, INTENT(OUT) :: r b = 0 IF (p > 3) THEN CALL DoSOmething(a,b,c) q = q + 1 WRITE(*,*) a, b, c r = 1 .......... ELSE IF (p < -3) THEN END PROGRAM Example3 q = q - 1 r = 2 ELSE r = 3 END IF END SUBROUTINE DoSomething