FlashBASIC C functions overview

C functions can be called from a FlashBASIC program or subroutine using a syntax similar to that of normal C.

For Windows: The FlashBASIC program or subroutine must be compiled with the optimized (o option.

Note: See the Reference Manual for your specific operating system for information on any C Functions that are not represented in this guide.

Commonly used functions are included in the run time package and require no special linking. These functions are referred to as built-in functions. A user-defined function can also be included in a FlashBASIC application by linking it with the monitor, thus making a user-defined built-in function.

C function calls can be embedded in FlashBASIC expressions, FlashBASIC statements or can be used as arguments to other C function calls. Combined size of all parameters passed cannot exceed 32 KB. This size can be changed, however, by the TCL set-cmem command.

FlashBASIC expressions and FlashBASIC statements should not be used as arguments to FlashBASIC C functions.

If a type specifier is omitted before an argument name, the argument is assumed to be of type integer when arg is a number, or to be of type pointer when arg is a dynamic array (string). If type is omitted before the function name, the function is assumed to be of type integer.

arg0...argn are the optional arguments to the function; up to 15 arguments are supported.

The return type of a function is void if the function is not part of an assignment statement or used as an argument to another C function or FlashBASIC statement. Whenever the return value of a function can be neglected, the type can be overridden by (void).

When a call to a function is made:

  • All arithmetic arguments are passed by value.
  • All dimensioned arrays are passed by reference.
  • All dynamic arrays (strings) are passed by reference.
  • String constants (strings between quotation marks) and sub-strings (string[m,n}) cannot be modified.

    If a C function tries to modify a string beyond the end of the string, the string is truncated to its original size, but no error is reported. As is usual in C, no data translation occurs implicitly.

  • Pointers are all treated as pointers to characters.

    When assigned to a FlashBASIC variable, a pointer can only be passed to another C function or be tested for the value 0 (null pointer), anything other than this makes no sense. The following statement has unpredictable results:

 ptr=(char*)%malloc(1024)
 if ptr > 0 then print ’ok’

The only valid form is to test for ptr = 0 or ptr # 0.

Argument Types

These argument types are supported:

Argument Type Description
<nothing> Default. If arg is a number, an (int) is passed to the function. If arg is a dynamic array (string), a pointer to the first character of the string is passed to the function. If arg is a dimensioned array, a pointer to an image of the dimensioned array is passed to the function. See Passing Dimensioned Arrays below.
(int) Integer. The arg is divided by the precision factor and passed to the C function. Integers are 32-bit signed elements.
(char) Character. If arg is a dynamic array (string), the first character is passed to the C function. If arg is a number it is divided by the precision factor and the result is truncated to 8 bits and passed to the C function.
(char*) Pointer. The arg is a number that is passed to the C function without being divided by the precision factor. The only legal use of this type is to pass a null pointer or a pointer returned by a previous call to a C function to another C function.

All types, except pointers can be prefixed by the keyword unsigned, (unsigned char, for example).

Note: The precision factor is a number between 0 and 9, with 4 being the default value. When a number is divided by the precision factor, the number is actually being divided by 10 raised to the power of precision.

Passing Dimensioned Arrays

Dimensioned arrays of integers can be passed to an external C function. When the array is passed, the C function receives a pointer to the array and all the integers in the array are divided by the precision factor. If the array is multidimensional, the integers are organized column by column. For example, if we have an array that is dimensioned to (2, 5) in FlashBASIC, the values in the C array would be in this order: (1, 1), (2, 1), (1, 2), (2, 2), (1, 3), (2, 3), and so on.

Function Types

These function types are supported:

Function Description
<nothing> Default. The return value is assumed to be an integer. It is multiplied by the precision factor.
(void) Return value is discarded. When prefixed with this type, the function cannot be part of an assignment or be used as an argument to another C function or FlashBASIC statement or function.
(int) Integer. The return value is a signed integer. It is multiplied by the precision factor.
(char) Character. The return value is stored as a dynamic array containing only one character.
(char*) Pointer. The return value is stored without being multiplied by the precision factor. The only legal use of this type is to store a pointer that for use as an argument to another C function.

’Address of’ Unary operator

The C unary operator, &, is used to pass the address of an integer to an external C function. This is the only valid form. Note that FlashBASIC converts numbers that are too large into strings. In this case, the C function would receive a pointer to a character instead of a pointer to an integer.

Space Reserve command

When a FlashBASIC variable (a dynamic array or an element of a one or two-dimensional array) is used to store data returned by a call to a C function (by means of a pointer passed as an argument), the FlashBASIC variable must have been assigned a size before the call to the function. This needs to be done because C has no notion of dynamic arrays. If a size is not assigned before the result of a C function call is stored in the variable, the data is truncated. For example:

 char var[size] {,var[size],...}

This reserves at least size bytes for var. size can be a constant or an expression. var can be a dynamic array or an element of a one or two-dimensional array. After reserving space for var, the content of the variable is undefined.

If a string longer than size is assigned to a variable, it is truncated and the characters beyond the given size are ignored. If a string shorter than size is assigned to a variable, the remainder of the char buffer remains undefined. The content of the buffer, including how it is terminated, is determined by the function. A terminator is not automatically added.

The buffer should not be used for any purpose other than as a C function call parameter. Doing so may change the internal format of the variable such that it is no longer a char buffer. If the data in the buffer needs further processing by BASIC, it should be copied to another variable and the copy used. For an example, see the %read() function example code.

Static Data

When using user-defined built-in functions, static data defined as part of a user-defined function remains valid as long as the D3 process is not disconnected from the virtual machine. This is true even if the FlashBASIC program is finished executing and has returned to TCL. The scope of static data lasts longer than in conventional UNIX or Windows programs.

Because static data takes up space in the data section of each D3 process (therefore main memory), it is not advisable to have large amounts of static space.

Includes

The following header files are provided in the file bp,unix.h, in the dm account.

errno.h errno values
fcntl.h Codes for %open(), %creat
ipc.h Semaphores, shared memory, messages
mode.h File access codes
sem.h Semaphore operations
signal.h UNIX signal numbers

These rules apply when making a call to a C function:

  • Parameters for each function must be enclosed in parentheses.
  • There must be no spaces between the %, the function name, and the (.
  • function is assumed to return an integer unless explicit casting is performed.

Syntax

 {variable =}{(type)}%function({{(type)} arg0{,...{(type)} argn}})