The way of using a user-defined function is exactly identical to that of using Fortran intrinsic functions. One can use a function in an expression and in a WRITE. Suppose we have the following function:
This function takes three REAL formal arguments and returns their average.REAL FUNCTION Average(x, y, z) IMPLICIT NONE REAL, INTENT(IN) :: x, y, z Average = (x + y + z) / 3.0 END FUNCTION Average
To use this function, one needs to supply values to the formal arguments. For example, one could write the following:
The above expression involves the use of function Average. Since this function has three formal arguments, three values must be presented to the function. Here, the values are 1.0, 2.0 and 3.0 and the returned value is the average of these three numbers (i.e., 2.0). The values or expressions used to invoke a function are referred to as actual arguments...... = ..... + Average(1.0, 2.0, 3.0) * .....
Please keep the following important rules in mind:
The first line has more actual arguments than the number of formal arguments, and the second line has less actual arguments than the number of formal arguments...... = ... + Average(1.0, 2.0, 3.0, 4.0) * .... ..... = ... - Average(3.0, 6.0) + .....
In the above example, the first actual argument is an INTEGER which doe not match with the type of the first formal argument. Thus, it is incorrect.WRITE(*,*) Average(1, 2.5, 3.7)
In the above, the first line shows the use of constants as actual arguments. The second line uses variables, while the third uses expression. In the third line, the result of evaluating a+b, b*c and (b+c)/a will be supplied to the three formal arguments of function Average().REAL :: a = 1.0, b = 2.0, c = 3.0 ..... = ... + Average(1.0, 2.0, 3.0) + ..... ..... = ... + Average(a, b, c) + ..... ..... = ... + Average(a+b, b*c, (b+c)/a) + .....