[Next] [Up] [Previous]

The FUNCTION Statement

The FUNCTION statement
(FUN)

A typical FUNCTION statement might be

       FUNCTION(DOUBLE_PRECISION) ZTR((REAL)X:(CHARACTER*4)LV,
            (INTEGER)JJH,%[(INTEGER)KK,])

This function declares the program to be compiled to be a function with the name ZTR and a double-precision result. Its arguments are X, LV, JJH, and KK, declared to be REAL, CHARACTER*4, INTEGER, and INTEGER respectively. Since LV is separated from X by a colon, LV can be omitted from a call to ZTR by omitting both the variable and its preceding punctuation.

The argument brackets preceded by a percent sign surrounding the last argument indicate that the call to ZTR may contain more than four arguments. Extra arguments, separated by commas, will be assigned to the internal argument KK one after the other, under control of statements to be described later.

Note that %[...] could have been replaced by %?(...?)`; in other words, the % sign has not created a new token within which substitutes cannot be used.

As the FUNCTION statement describes how a function header looks from the 'inside', the TEXT attribute can be replaced by the STRING type in such headers. The EQUATE attribute is also unnecessary; although variables in the function call are assigned to arguments based on their names, they are passed to the subroutine based on their order: this attribute represents a method of writing subprogram calls, not a method of parameter passing. This does not apply, of course, to the LIST_EQUATE attribute, but the LIST type can be used instead in a FUNCTION or SUBROUTINE statement with no error. The MATHEMATICAL attribute can be used: in the routine, it causes attempts to assign to a variable to be flagged as errors. In an XFUN or PFUN statement, it does not prevent such attempts, but only ensures that they will not change the value of the variable after return from the routine is made. Thus, PFUN does not give the MATHEMATICAL attribute the sum of its meanings in the XFUN and FUNCTION statements.

Special functions are available within subprograms to manage their parameter lists. Here, _NUMBER(KK) would return the number of different arguments to substitute for KK; _PRESENT(LV) would indicate if LV were present in the argument list (a call ZTR(A,B,C,D) would assign A to X, leave LV omitted, assign C to JJH, and make D the only value for KK).

The statement

    27 GET KK

would assign a value to the parameter KK from the parameter list, and the statement

       REPEAT 27

would then return for the next value of KK, this being another use of the REPEAT statement, in addition to its use with the FOR statement.

Within a function, the name of a function refers to its eventual result, and may be used on both sides of the equals sign as a variable of the same type as the function's result. Because of the existence of the AUTO storage class in FALCON, you may reasonably expect that functions and subroutines are also to be able to call themselves. The predefined identifier _SELF acts as the name of a function as a function from within itself for this purpose.

The statement

       FUNCTION VVJ((REAL)X(4))

indicates that the function VVJ has one argument, an array of REAL numbers with four elements. It is sometimes desired to pass, as function arguments, arrays with an arbitrary size. This can be done using variables of MUTABLE type; however, a method is also provided to do this with arrays that are of an ordinary type in the calling program without use of the MUTABLE type, for compatibility with FORTRAN, and with assembly-language programs in which the complexity of use of MUTABLE variables is desired to be avoided, and to avoid the overhead of MUTABLE variables.

This is done by a statement like

       FUNCTION YYK((REAL)M(,),I,J)

which declares M to be a two-dimensional array argument with dimensions assignable at execution time. The two additional INTEGER arguments I and J are to be used to pass the dimensions of M explicitly, as it is to be two-dimensional.

Before the array argument M is used in any calculation, the statement

       CALL _SETDIM(M,_A(I,J))

is used to determine how the array M will be referenced. (It is still necessary to declare M to be two-dimensional, so that references to M with omitted subscripts can be detected and made to refer to vectors.)

The call to _SETDIM is not strictly necessary if the array is one-dimensional and no array bounds checking is performed.

A period following the FUNCTION statement keyword indicates that all variables in the function are to be of AUTO storage class unless otherwise declared. This also applies to the keyword SUBROUTINE in the statement following.


[Next] [Up] [Previous]