Skip to content
Andrew Owen edited this page May 30, 2023 · 2 revisions

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.

Welcome to the SE BASIC wiki


  • Press RETURN to return to this menu.
  • Enter the name of a TOPIC to display it.
  • Enter EXIT to return to BASIC.

Choose from the following topics:

  1. Quick start guide: QUICK
  2. User's guide: USER
  3. Configuration guide: CONFIG
  4. Language guide: LANGUAGE
  5. Language reference: BASIC
  6. Technical reference: TECH
  7. Developer's guide: DEV
  8. Acknowledgemets: CREDITS
  9. Licenses: LICENSE
Clone this wiki locally