To call a C function from a Calc program, from a C source file or library, follow this procedure:


1. Declare the function at the top of the Calc source file using a format similar to the below:


#<external c># function int function1( string arg1, array of int [100] arg2, int arg3 )

This example declaration has three arguments.

The declation must start with the '#<external c>#' keyword.

The function can return any simple type but not an array or struct type. If multiple return values are required this can be accomodated by passing in a parameter which is an array or struct type.


2. Create the C source file for the interface.

Declare the C function in a similar way to the below.

#include "calc.h"

char *_calc_function1( var *result, var *arg1, var *arg2, var *arg3, char *source_file_id_txt )


The function name must have the text "_calc_" added to the start of it.

If the function returns an Calc type 'int', 'decimal' or 'bool' the C function should have a return type of 'long int'.

If the function returns a string, the C function should have a return type of 'char *', and an additional
first parameter of 'char *result' must be added to the function declaration.

Set the value of the return string using: 

'_calcsys_var_set_s( (var *) result, char *s, int is_constant_string, 0 );'


Additionally the last statement of the function must be 'return (result);'

A final parameter 'char *source_file_id_txt' is added to all function calls, containing the source code filename
and line number of the calling location for error reporting purposes.




3. Compile the C source file and include the ".o" object file when linking in to the Calc executable file.



