Argument Association

When using a function, the values of actual arguments are passed to their corresponding formal arguments. There are some important rules:

Example

We shall use the following function to illustrate the above rules. Function Small() takes three formal arguments x, y and z and returns the value of the smallest.
INTEGER :: a, b, c            INTEGER FUNCTION  Small(x, y, z)
                                 IMPLICIT  NONE
a = 10                           INTEGER, INTENT(IN) :: x, y, z
b = 5
c = 13                           IF (x <= y .AND. x <= z) THEN
WRITE(*,*) Small(a,b,c)             Small = x
WRITE(*,*) Small(a+b,b+c,c)      ELSE IF (y <= x .AND. y <= z) THEN
WRITE(*,*) Small(1, 5, 3)           Small = y
WRITE(*,*) Small((a),(b),(c))    ELSE
                                    Small = z
                                 END IF
                              END FUNCTION  Small

In the first WRITE, all three arguments are variable and their values are sent to the corresponding formal argument. Therefore, x, y and z in function Small() receives 10, 5 and 13, respectively. The following diagram illustrate this association:

In the second WRITE, the first and second arguments are expression a+b, b+c. Thus, they are evaluated yielding 15 and 18. These two values are saved in two temporary locations and passed to function Small() along with variable c. Thus, x, y and z receive 15, 18 and 13 respectively. This is illustrated as follows. In the figure, squares drawn with dotted lines are temperary locations that are created for passing the values of arguments.

In the third WRITE, since all actual arguments are constants, their values are "evaluated" to temporary locations and then sent to x, y and z.

In the fourth WRITE, since all three actual arguments are expressions, they are evaluated and stored in temporary locations. Then, these values are passed to x, y and z.

Thus, x, y and z receive 10, 5 and 13, respectively.

Note that while the formal arguments of function Small() receive the same values using the first and the fourth lines, the argument associations are totally different. The first line has the values in variables passed directly and the the fourth line evaluates the expressions into temporary locations whose values are passed. This way of argument association does not have impact on functions (since you must use INTENT(IN)), it will play an important role in subroutine subprograms.