next up previous contents index
Next: SAVE Statement Up: Procedures Previous: RETURN Statement

FUNCTION Statement

  The FUNCTION statement must be the first statement of every external function. Its general form is:
type FUNCTION( dummy1, dummy2, ... dummyN )
The type specification is optional: if it is omitted then the type of the result is determined by the usual rules. The function name may have its type specified by a type or IMPLICIT statement which appears later in the program unit. If the function is of type character then the length may be specified by a literal constant (but not a named constant) or may be given in the form CHARACTER*(*) in which case the length will be passed in as the length declared for the function name in the calling program unit.

There may be any number of dummy arguments including none, but the parentheses must still be present. Dummy arguments may, as described in section 9.4, be variables, arrays, or procedures.

The function name may be used as a variable within the function subprogram unit; a value must be assigned to this variable before the procedure returns control to the calling unit. If the function name used the passed-length option then the corresponding variable cannot be used as an operand of the concatenation operator except in an assignment statement. The passed-length option is less useful for character functions than for arguments because the length is inevitably the same for all references from the same program unit. For example:

 
       PROGRAM FLEX 
       CHARACTER CODE*8, CLASS*6, TITLE*16  
       CLASS = CODE('SECRET') 
       TITLE = CODE('ORDER OF BATTLE') 
       END 

       CHARACTER*(*) FUNCTION CODE(WORD) 
       CHARACTER WORD*(*), BUFFER*80 
       DO 15, K = 1,LEN(WORD) 
            BUFFER(K:K) = CHAR(ICHAR(WORD(K:K) + 1) 
15     CONTINUE 
       CODE = BUFFER 
       END
Unfortunately, although this function can take in an argument of any length up to 80 characters long and encode it, it can only return a result of exactly 8 characters long when called from the program FLEX, so that it will not produce the desired result when provided with the longer character string. This limitation could be overcome with the use of a subroutine with a second passed-length argument to handle the returned value.

Functions without arguments do not have a wide range of uses but applications for them do occur up from time to time, for example when generating random numbers or reading values from an input file. For example:

 
       PROGRAM COPY 
       REAL NEXT 
       DO 10,I = 1,100 
             WRITE(UNIT=*,FMT=*) NEXT() 
10     CONTINUE 
       END 

       REAL FUNCTION NEXT()  
       READ(UNIT=*,FMT=*) NEXT  
       END
The parentheses are needed on the function call to distinguish it from a variable. The function statement itself also has to have the empty pair of parentheses, presumably to match the call.


next up previous contents index
Next: SAVE Statement Up: Procedures Previous: RETURN Statement

Mario Storti
Wed Nov 4 19:32:56 ART 1998