DEF_FN - source-solutions/HELP GitHub Wiki

DEF FN


DEF FN[ ]name [( arg_0 [, arg_1] ...)] = expression

Defines a function called FN name (or FN name: spaces between FN and name are optional). On calling FNname( ... ), expression is evaluated with the supplied parameters substituted. Any variable names used in the function that are not in the argument list refer to the corresponding global variables. The result of the evaluation is the return value of FNname. The type of the return value must be compatible with the type indicated by name.

Example

Create the recursive function FN F(n) to calculate the factorial for n:

DEF FN F(N)=VAL (({N*FN F(N-1)} AND N)+({1} AND (N=0)))

Notes

  • This statement may only be used on a program line.
  • As the function must be a single expression and SE Basic IV does not have a ternary operator, the only way to define a recursive function that actually terminates is by using VAL or VAL$.

Parameters

  • name must be a legal variable name.
  • arg_0, arg_1, ... must be legal variable names. These are the parameters of the function. Variables of the same name may or may not exist in the program; their value is not affected or used by the defined function.
  • expression must be a legal SE Basic IV expression.

Errors

  • The statement is executed directly instead of in a program line: Illegal direct.
  • If the type of the return value is incompatible with the type of name, no error is raised at the DEF FN statement; however, a Type mismatch will be raised at the first call of FNname.
⚠️ **GitHub.com Fallback** ⚠️