DEFFUN Statement
Declares an external function so that it can be called by a DataBasic program.
Syntax
DEFFUN {name}({parameterList})
Syntax Elements
name The name of the function.
parameterList A comma-separated list of parameters, representing the values passed to the function. parameterList must be identical to that in the FUNCTION statement in the function's definition.
Each parameter has the following syntax:
{MAT }paramName
MAT Specifies that the parameter is a dimensioned array. The array must be dimensioned before being used within the function.
paramName The parameter name. Parameter names must obey the rules for identifiers. The names used do not have to be the same as in the function definition.
Comments
An external function must be declared with a DEFFUN statement before it can be used. It is then executed in the same way as an intrinsic function; that is, its returned value must be used in some way (it cannot be executed with the CALL statement). The number and types of parameters passed to the function must match those declared in the FUNCTION and DEFFUN statements. If there is a mismatch, an error message is displayed and the program enters the debugger.
Note that you can call an external function recursively without declaring it within the function code.
Examples
XOR Function
In the item XOR:
**************************************
* Function XOR
*
* Returns the result of performing an XOR operation on two Boolean values.
*
**************************************
FUNCTION XOR(B1, B2)
RETURN((B1 AND NOT(B2)) OR (B2 AND NOT(B1))
END
In the item XORPROG:
* Declare the XOR function. DEFFUN XOR(BOOL1, BOOL2) . . . BLN1 = @TRUE BLN2 = @FALSE * Call the XOR function. XORVAR = XOR(BLN1, BLN2)
The latter program fragment calls the XOR function to perform an exclusive OR on the Boolean values held in variables BLN1 and BLN2 and sets the variable XORVAR to the result.
ADD Function
FUNCTION ADD (X, Y)
* Declare the ADD function.
DEFFUN ADD(X, Y)
.
.
.
NEWVAL = ADD (A+2, 395)
Function ADD is passed two parameters. On return to the calling routine, the result is assigned to variable F.
ARYFUN Function
FUNCTION ARYFUN(MAT ARY, INDEX)
DIM ARY(10)
* Declare the ARYFUN function. DEFFUN ARYFUN(MAT ARY, INDEX) DIM MYDATA(10) . . . CALL ARYFUN(MAT MYDATA, ELEMENT)
Function ARYFUN accepts and returns two values, the first of which must be a dimensioned array.
See Also
FUNCTION statement.