User-defined functions are possible and behave the same way, with the only exception that user-defined functions cannot currently have optional arguments.
Altering the variables that are used as arguments within the function will cause the same changes to the variables outside of the function. This also applies to subs. Variables that are not arguments of the function will not be changed if used within the function unless they are SHARED. See the second example of subs for a demonstration of this (it functions the same way in subs).
The arguments of a function need to be enclosed with parantheses (unless there are no arguments).
Function and sub procedures are placed after the main code body.
Example 1: Some BASIC functions.
Example 2: User-defined function.
- Explanation: The calculated value is assigned to the name of the function. When calculations require several changes, use an intermediary variable. Then assign that value to the function name.
- SUB. FUNCTION
- Statement, Sub (explanatory)
- DEF FN - the old way of defining functions, mostly for backwards compatibility.