FUNCTION xxx (arg1 [,arg2, …])
<statements>
xxx = <return value>
Defines a callable function. This is the same as adding a new function to MMBasic while it is running your program.
‘xxx’ is the function name and it must meet the specifications for naming a variable. ‘arg1’, ‘arg2’, etc are the arguments or parameters to the function.
To set the return value of the function you assign the value to the function’s name.
1 2 3 |
FUNCTION SQUARE(a) SQUARE = a * a END FUNCTION |
Every definition must have one END FUNCTION statement. When this is reached the function will return its value to the expression from which it was called. The command EXIT FUNCTION can be used for an early exit.
You use the function by using its name and arguments in a program just as you would a normal MMBasic function.
1 |
PRINT SQUARE(56.8) |
When the function is called each argument in the caller is matched to the argument in the function definition. These arguments are available only inside the function.
Functions can be called with a variable number of arguments. Any omitted arguments in the function’s list will be set to zero or a null string.
Arguments in the caller’s list that are a variable (ie, not an expression or constant) will be passed by reference to the function. This means that any changes to the corresponding argument in the function will also be copied to the caller’s variable.
You must not jump into or out of a function using commands like GOTO, GOSUB, interrupts, etc. Doing so will have undefined side effects.
See also:
END FUNCTION
END SUB
EXIT FUNCTION
EXIT SUB
LOCAL
SUB
Leave a Reply