# Using Functions

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:

```REAL FUNCTION  Average(x, y, z)
IMPLICIT  NONE

REAL, INTENT(IN) :: x, y, z

Average = (x + y + z) / 3.0
END FUNCTION Average
```
This function takes three REAL formal arguments and returns their average.

To use this function, one needs to supply values to the formal arguments. For example, one could write the following:

```..... = ..... +  Average(1.0, 2.0, 3.0)  * .....
```
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.

Please keep the following important rules in mind:

• The number of formal arguments and actual arguments must be equal.
```..... = ... + Average(1.0, 2.0, 3.0, 4.0) * ....
..... = ... - Average(3.0, 6.0) + .....
```
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.
• The type of the corresponding actual and formal arguments must be identical.
```WRITE(*,*) Average(1, 2.5, 3.7)
```
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.
• The actual arguments can be constants, variables and even expressions.
```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) + .....
```
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().
Please continue with the next page on argument association