User-defined functions

User-defined functions (FN) work like the built-in functions — ABS(n), MOD(a,b), LEFT$(a$,n), and so on — except that the user defines what the function does. Defining new FN functions is something like extending the definition of the BASIC language. The FN function contains code that can be called from several places in the program, each time returning to the place from which it was called on that occasion. (The GOSUB is another way to write code to be called from multiple places.)

Location. An FN function must be defined earlier in the program than any of the places that call it. Generally, a program consists of all the FN functions, followed by the main program.

Nesting/recursion. An FN function can call another FN function, and can even call itself (directly or through intermediate functions). Be sure that, when an FN function calls itself, you take care that the process does not repeat indefinitely. Each call requires memory from the execution stack, which is part of the memory that Android allocates to your program; making function calls without limit will eventually make the program fail for lack of memory.

Name. When creating an FN function, you specify its name. The rules for naming FN functions are the same as for naming variables.