Next: Statement Function Rules Up: Procedures Previous: Intrinsic Functions

Statement Functions

Statement functions can be defined within any executable program unit by means of statement function statements. They can only be used, however, within the same program unit. Although statement functions have limited uses, they are unjustly neglected by many programmers.

The statement function statement resembles an ordinary assignment statement. For example:
` FAHR(CELS) = 32.0 + 1.8 * CELS`
The function FAHR converts a temperature in degrees Celsius to its equivalent in Fahrenheit. Thus FAHR(20.0) would return a value 68.0 approximately.

A statement function can have any number of dummy arguments (such as CELS above) all of which must appear in the expression on the right-hand side; this expression may also include constants, variables, or array elements used elsewhere in the program. When the function is called the current values of these items will be used. For example:

```
REAL M1, M2, G, R
NEWTON(M1, M2, R) = G * M1 * M2 / R**2
```

A reference to the function in an assignment statement such as:
` FORCE = NEWTON(X, Y, DIST) `
will return a value depending on the values of the actual arguments X, Y, and DIST, and that of the variable G at the time the function is referenced.

Definitions of statement functions can also include references to intrinsic functions, external functions, or previously defined statement functions:

```
PARAMETER (PI = 3.14159265, DTOR = PI/180.0)
SIND(THETA) = SIN(THETA * DTOR)
COSD(THETA) = COS(THETA * DTOR)
TAND(THETA) = SIND(THETA) / COSD(THETA)
```
These definitions allow trigonometry on angles specified in degrees rather than radians.

The scope of each dummy argument name (such as THETA above) is that of the statement alone; these names can be used elsewhere in the program unit as variables of the same data type with no effect whatever on the evaluation of the function.

Statement functions can have any data type; the name and arguments follow the normal type rules. They can be useful in character handling, for example:

```
LOGICAL MATH, DIGIT, DORM
CHARACTER C*1
DIGIT(C) = LGE(C, '0') .AND. LLE(C, '9')
MATH(C)  = INDEX('+-*/', C) .NE. 0
DORM(C)  = DIGIT(C) .OR. MATH(C)
```
These three functions each return a logical value when presented with a single character argument: DIGIT tests to see whether the character is a digit, MATH whether it is an operator symbol, and DORM will test for either condition. Note the use of the lexical comparison functions LGE and LLE in the definition of DIGIT which make it completely independent of the local character code.

Next: Statement Function Rules Up: Procedures Previous: Intrinsic Functions
Helen Rowlands
8/27/1998