G2Basic 0.1.1 (1e793fc)
BASIC interpreter for microcontrollers
Loading...
Searching...
No Matches
Custom functions

Making C functions callable from BASIC expressions. More...

Collaboration diagram for Custom functions:

Functions

int g2basic_register_function (const char *name, int arg_count, double(*func_ptr)(double[], int))
 Make a C function callable from BASIC expressions.
 

Detailed Description

Making C functions callable from BASIC expressions.

Function Documentation

◆ g2basic_register_function()

int g2basic_register_function ( const char *  name,
int  arg_count,
double(*)(double[], int)  func_ptr 
)

Make a C function callable from BASIC expressions.

BASIC code calls it as name(arg, ...). The interpreter checks the argument count before calling. It passes at most 8 arguments; more is the error too many function arguments. There is no error channel for the result: a NaN result is an ordinary value. Assigning NaN to a variable makes that variable read as undefined.

Parameters
nameName used in BASIC; the string is copied. Names are case-sensitive, and the built-in functions are lowercase (sqrt). The name must be a valid identifier (a letter or underscore, then letters, digits, or underscores) to be callable; this is not validated. Must not be NULL.
arg_countRequired number of arguments from 0 to 8, or -1 to accept any number up to 8. A call with a different count fails with the error "function 'name' expects N arguments, got M".
func_ptrImplementation. It receives the evaluated arguments and their count and returns the result. Must not be NULL; this is not checked.
Return values
0The function is registered.
-1The name is already registered (built-in functions included), or memory allocation failed. An existing function is never replaced.
static double square(double args[], int count) {
(void)count; // Checked by the interpreter: always 1.
return args[0] * args[0];
}
g2basic_register_function("square", 1, square);
// BASIC: PRINT square(5) -> 25
int g2basic_register_function(const char *name, int arg_count, double(*func_ptr)(double[], int))
Make a C function callable from BASIC expressions.