FUNCTION:
Factor handbook » C library interface » Calling C from Factor

Prev:LIBRARY:
Next:FUNCTION-ALIAS:


Vocabulary
alien.syntax

Syntax
FUNCTION: return name ( parameters )


Inputs
None

Outputs
None

Word description
Defines a new word name which calls the C library function with the same name in the logical library given by the most recent LIBRARY: declaration.

The new word must be compiled before being executed.

For a variadic C function, put ... between the named and unnamed parameters. List the concrete types and names for every argument at this call site. For example, FUNCTION: int printf ( c-string format, ... int value ). Unnamed float arguments round to binary32 before promotion to double; integer types narrower than int are narrowed according to their declared type before promotion to int. Boolean and enum arguments retain their source conversion before promotion. A declaration has at most one marker. The declaration specifies one typed call shape; it does not infer argument types from a printf format string.

Examples
For example, suppose the foo library exports the following function:
void the_answer(char* question, int value) { printf("The answer to %s is %d. ",question,value); }

You can define a word for invoking it:
LIBRARY: foo FUNCTION: void the_answer ( c-string question, int value ) "the question" 42 the_answer
The answer to the question is 42.

Using the c-string type instead of char* causes the FFI to automatically convert Factor strings to C strings. See C strings for more information on using strings with the FFI.

Notes
To make a Factor word with a name different from the C function, use FUNCTION-ALIAS:.

See also
FUNCTION-ALIAS:

Definition