SuivantPrec.Bas prec.BasNiv. sup.

9.2 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.

Statement Function Rules  

Statement function statements must appear after any the specification statements but before all executable statements in the program unit. They may be intermixed with DATA and FORMAT statements. The general form is:        function ( dummy1, dummy2, ... dummyN ) = expression The function may have any data type; the expression will normally have the same data type but if both have an arithmetic type then the normal conversion rules for arithmetic assignment statements apply.

The name of the function must be distinct from all other symbolic names in the program unit. It may appear in type statements but not in other specification statements. (There is one exception: a common block is permitted to have the same name as a statement function but since common block names always appear between slashes there is little risk of confusion). If the function has character type its length must be an integer constant expression.

The dummy arguments are simply symbolic names. A name may not appear more than once in the same list. These names may be used elsewhere in the program unit as variables of the same data type.

The expression must contain the dummy arguments as operands. The operands may also include:

Note that character substrings are not permitted. The variables and array elements used in the expression must be defined at the time that the function reference is executed.

Guidelines 

Although statement functions have a limited role to play in programs because they can only be defined in a single statement, references to statement functions they may be executed more efficiently than references to external functions; many modern compilers expand statement function references to in-line code when it is advantageous to do so.

If the same statement function is needed in more than one program unit it would is possible to use an INCLUDE facility to provide the same definition each time, but it will usually be better to use an external function instead.

SuivantPrec.Bas prec.HautNiv. sup.