FunctionCompile
✖
FunctionCompile
generates a list of compiled code functions from a list of pure functions.
generates an association of compiled code functions from an association of Wolfram Language functions.
Details and Options



- The function f is typically specified as a Function pure function. The variables in the Function object are typically annotated with Typed.
- The CompiledCodeFunction objects created by FunctionCompile can be applied to suitable arguments just like the uncompiled functions.
- Inside f, KernelFunction can be used to indicate functions that should directly use the Wolfram Engine rather than being compiled into low-level code.
- Inside f, DownValuesFunction can be used to compile definitions attached to a symbol.
- The function f can be specified purely as the name of a function that exists as a declaration in the compiler environment.
- In FunctionCompile[defs,fspec], calls from fspec can be made to local auxiliary definitions defs. These definitions are only used for this call to FunctionCompile.
- Auxiliary definitions used inside fspec can also be provided by giving a CompilerEnvironment option.
- The code in the CompiledCodeFunction object can be output for external purposes using FunctionCompileExportLibrary and related functions.
- When the option TargetSystem is used to specify additional architectures, this helps the output to be moved between different platforms without having to run the compilation again.
- The following options can be given:
-
CompilerEnvironment Automatic an environment of definitions for compilation CompilerOptions Automatic detailed options for the compilation pipeline CompilerRuntimeErrorAction Automatic the behavior when there is an unrecoverable error while executing low-level code ProgressReporting Automatic how to report progress during the compilation TargetSystem Inherited machine architectures for code generation UseEmbeddedLibrary False create and embed a shared library for this architecture - Possible settings for TargetSystem are:
-
Automatic generate additional code for key machine architectures All generate additional code for all machine architectures trg generate additional code for machine architecture trg {trg1,trg2,…} generate additional code for machine architectures {trg1,trg2,…} - The possible values of TargetSystem are given in the list $TargetSystems.
- Possible values for CompilerRuntimeErrorAction are:
-
Automatic rerun the computation in the Wolfram Engine if it avoids compiler-specific code "Evaluate" always rerun the computation in the Wolfram Engine None return a failure object that represents the error fun apply fun to the failure object and return the result
Examples
open allclose allBasic Examples (6)Summary of the most common use cases
Compile a Function into a compiled code function:

https://wolfram.com/xid/0i1l6t7rm-ck3oom

The CompiledCodeFunction evaluates with an argument of the correct type:

https://wolfram.com/xid/0i1l6t7rm-lj8jw2

If unexpected arguments are given, an error results:

https://wolfram.com/xid/0i1l6t7rm-vyw6nk


Many common operations are supported in compiled code:

https://wolfram.com/xid/0i1l6t7rm-nyeyo3


https://wolfram.com/xid/0i1l6t7rm-kevm7k


https://wolfram.com/xid/0i1l6t7rm-3td1u1


https://wolfram.com/xid/0i1l6t7rm-7ahy10

Information can extract the type of the CompiledCodeFunction:

https://wolfram.com/xid/0i1l6t7rm-1gxwf0

Several functions can be compiled with FunctionCompile:

https://wolfram.com/xid/0i1l6t7rm-xps8uh


https://wolfram.com/xid/0i1l6t7rm-6shpp7

It can be useful to use an association to hold the functions:

https://wolfram.com/xid/0i1l6t7rm-siwiy


https://wolfram.com/xid/0i1l6t7rm-gvuufv

Auxiliary definitions can be given with FunctionDeclaration:

https://wolfram.com/xid/0i1l6t7rm-g03


https://wolfram.com/xid/0i1l6t7rm-iqgrsb

New types can be added with TypeDeclaration:

https://wolfram.com/xid/0i1l6t7rm-b6mfdn
Compile a function that instantiates a product type and then extracts a field:

https://wolfram.com/xid/0i1l6t7rm-8f4kzn


https://wolfram.com/xid/0i1l6t7rm-2zmsdp

FunctionCompile will create a function from the name of a declaration in the compiler environment:

https://wolfram.com/xid/0i1l6t7rm-rkk9ze

https://wolfram.com/xid/0i1l6t7rm-krmevz


https://wolfram.com/xid/0i1l6t7rm-87gbvi

If FunctionCompile is unable to compile something, it issues a message that typically contains a button to show the source of the error:

https://wolfram.com/xid/0i1l6t7rm-egst63


Clicking the source button typically opens a user interface that can help to locate the error:

https://wolfram.com/xid/0i1l6t7rm-3owia0
Scope (12)Survey of the scope of standard use cases
A wide range of native types are supported:

https://wolfram.com/xid/0i1l6t7rm-culzes


https://wolfram.com/xid/0i1l6t7rm-n2xybm


https://wolfram.com/xid/0i1l6t7rm-ny83c7


https://wolfram.com/xid/0i1l6t7rm-g18hht


https://wolfram.com/xid/0i1l6t7rm-kpewx1

Compound types are also supported:

https://wolfram.com/xid/0i1l6t7rm-kxar7k


https://wolfram.com/xid/0i1l6t7rm-bj3avc

Function types are also supported:

https://wolfram.com/xid/0i1l6t7rm-re18oz

https://wolfram.com/xid/0i1l6t7rm-x3lxve
Passing in the function and integer arguments carries out the required computation:

https://wolfram.com/xid/0i1l6t7rm-2bgit8

The Wolfram Engine can be called using KernelFunction. This defines a function in the Wolfram Engine:

https://wolfram.com/xid/0i1l6t7rm-nobfu5
Use KernelFunction to call the user-defined function from compiled code:

https://wolfram.com/xid/0i1l6t7rm-ovnc84


https://wolfram.com/xid/0i1l6t7rm-0cmlw4

Functions can be passed around as data:

https://wolfram.com/xid/0i1l6t7rm-c19cmx
The first argument selects which nested function is used:

https://wolfram.com/xid/0i1l6t7rm-equn9e


https://wolfram.com/xid/0i1l6t7rm-76u9z1

Nested functions can refer to variables in an outer scope:

https://wolfram.com/xid/0i1l6t7rm-u7j8dd

Nested functions that refer to variables in an outer scope can be used as function arguments:

https://wolfram.com/xid/0i1l6t7rm-583ekq


https://wolfram.com/xid/0i1l6t7rm-eqwam2

Print can be used to see the inside of a computation:

https://wolfram.com/xid/0i1l6t7rm-3t26w2

A CompiledCodeFunction can be passed as arguments to other CompiledCodeFunction expressions.

https://wolfram.com/xid/0i1l6t7rm-1hyxhc


https://wolfram.com/xid/0i1l6t7rm-skt5rb

This function takes a function as an argument:

https://wolfram.com/xid/0i1l6t7rm-ytlffm

This passes the first function as an argument to the second:

https://wolfram.com/xid/0i1l6t7rm-f6h27j

Work with functions that use := to set up DownValues for their declarations:

https://wolfram.com/xid/0i1l6t7rm-5w8nnd
The FunctionDeclaration gives the function a name and a type and states that the implementation comes from the DownValues:

https://wolfram.com/xid/0i1l6t7rm-38oyl1
Now a function that uses this declaration is compiled:

https://wolfram.com/xid/0i1l6t7rm-7ugg89

https://wolfram.com/xid/0i1l6t7rm-1wzmrs






A CompiledCodeFunction contains low-level code to allow serialization and deserialization on a machine of the same architecture:

https://wolfram.com/xid/0i1l6t7rm-3okfpf

Compilation results can be saved using Once:

https://wolfram.com/xid/0i1l6t7rm-y1cejk

This can be advantageous if the compilation is not very quick:

https://wolfram.com/xid/0i1l6t7rm-oihv98

https://wolfram.com/xid/0i1l6t7rm-gypkjw

Options (6)Common values & functionality for each option
CompilerEnvironment (1)
CompilerRuntimeErrorAction (1)
The behavior of errors when executing low-level code is governed by the CompilerRuntimeErrorAction:

https://wolfram.com/xid/0i1l6t7rm-1x62pk

When there is an error, the function is rerun in the evaluator:

https://wolfram.com/xid/0i1l6t7rm-tlqpj


If the function contains compiler-specific code, it is not rerun in the evaluator:

https://wolfram.com/xid/0i1l6t7rm-ldi21z


A setting of "Evaluate" always reruns in the evaluator:

https://wolfram.com/xid/0i1l6t7rm-6pl3qo




A setting of None never reruns in the evaluator:

https://wolfram.com/xid/0i1l6t7rm-0z566q


A function can be given to obtain custom behavior:

https://wolfram.com/xid/0i1l6t7rm-8ea4dq

ProgressReporting (1)
Progress during a compilation is reported:

https://wolfram.com/xid/0i1l6t7rm-6cy6wj

https://wolfram.com/xid/0i1l6t7rm-b570dn
This can be suppressed by setting the option ProgressReporting to False.
The default value of ProgressReporting is Automatic, which means that the global setting $ProgressReporting is used. If this is set to False, then no progress reporting takes place.
TargetSystem (2)
Create a compiled code function that can run on machine architectures additional to the current one:

https://wolfram.com/xid/0i1l6t7rm-vy17xx


https://wolfram.com/xid/0i1l6t7rm-y7iv7f

Create a compiled code function that can run on all possible machine architecture platforms:

https://wolfram.com/xid/0i1l6t7rm-wn0fe


https://wolfram.com/xid/0i1l6t7rm-bzbsjb

The compiled code function can be moved to machines with these $SystemID settings and it will be ready to run without having to recompile the original code.
Note that CreateCompilerEnvironment can be used to generate multiple target systems, and this can decrease the time to carry out cross-compilation.
UseEmbeddedLibrary (1)
Create a CompiledCodeFunction that embeds a shared library for the current platform:

https://wolfram.com/xid/0i1l6t7rm-w2cczx

The code function now includes an embedded library:

https://wolfram.com/xid/0i1l6t7rm-u2jpjj

The library will be used when the code function is loaded from a file. This makes loading faster:

https://wolfram.com/xid/0i1l6t7rm-4mm7to

The restored code function works as expected:

https://wolfram.com/xid/0i1l6t7rm-uvkfas

Properties & Relations (1)Properties of the function, and connections to other functions
TypeOf is a quick way to check if something may compile, because it just determines the type of its argument:

https://wolfram.com/xid/0i1l6t7rm-kegnam

Possible Issues (4)Common pitfalls and unexpected behavior
Errors (3)
If an error takes place while computing a function with the low-level code, the computation is run in the Wolfram Engine if it avoids compiler-specific code:

https://wolfram.com/xid/0i1l6t7rm-8w996j


https://wolfram.com/xid/0i1l6t7rm-qk1wrk

Here is an error while running the function. The computation is terminated, a message is issued and the Wolfram Engine is used to compute the result:

https://wolfram.com/xid/0i1l6t7rm-b6wua9


Integer constants are typed as the native machine integer:

https://wolfram.com/xid/0i1l6t7rm-vue7ab

Use Typed to specify a particular type:

https://wolfram.com/xid/0i1l6t7rm-y9btzd

Returning a function from CompiledCodeFunction is not supported:

https://wolfram.com/xid/0i1l6t7rm-4h8k4z


Creating Functions (1)
FunctionCompile cannot create a function if there is more than one declaration:

https://wolfram.com/xid/0i1l6t7rm-0obpzn

https://wolfram.com/xid/0i1l6t7rm-tziq7l


If Typed is used the compilation can work:

https://wolfram.com/xid/0i1l6t7rm-2bp3gs

FunctionCompile cannot create a function if there is a polymorphic declaration:

https://wolfram.com/xid/0i1l6t7rm-ij84rv

https://wolfram.com/xid/0i1l6t7rm-5fesml


If Typed is used the compilation can work:

https://wolfram.com/xid/0i1l6t7rm-1qhu9x

Neat Examples (2)Surprising or curious use cases
Nested functions can be used to implement recursive definitions:

https://wolfram.com/xid/0i1l6t7rm-ctj6i5


https://wolfram.com/xid/0i1l6t7rm-09vnmh

Declarations that use a symbol declaration are useful if the declaration is used more than once:

https://wolfram.com/xid/0i1l6t7rm-54a6c6
A FunctionDeclaration that refers to the symbol:

https://wolfram.com/xid/0i1l6t7rm-chk2hh
A function that uses the symbol:

https://wolfram.com/xid/0i1l6t7rm-3vycle
Compiled code for the function:

https://wolfram.com/xid/0i1l6t7rm-o1w4rd


https://wolfram.com/xid/0i1l6t7rm-w7lkob
A vector plot based on the data that was created:

https://wolfram.com/xid/0i1l6t7rm-9gkflt

The compiled version is much faster:

https://wolfram.com/xid/0i1l6t7rm-1dq2su


https://wolfram.com/xid/0i1l6t7rm-1rnr08


https://wolfram.com/xid/0i1l6t7rm-39u4qh

Wolfram Research (2019), FunctionCompile, Wolfram Language function, https://reference.wolfram.com/language/ref/FunctionCompile.html (updated 2021).
Text
Wolfram Research (2019), FunctionCompile, Wolfram Language function, https://reference.wolfram.com/language/ref/FunctionCompile.html (updated 2021).
Wolfram Research (2019), FunctionCompile, Wolfram Language function, https://reference.wolfram.com/language/ref/FunctionCompile.html (updated 2021).
CMS
Wolfram Language. 2019. "FunctionCompile." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/FunctionCompile.html.
Wolfram Language. 2019. "FunctionCompile." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/FunctionCompile.html.
APA
Wolfram Language. (2019). FunctionCompile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FunctionCompile.html
Wolfram Language. (2019). FunctionCompile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FunctionCompile.html
BibTeX
@misc{reference.wolfram_2025_functioncompile, author="Wolfram Research", title="{FunctionCompile}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/FunctionCompile.html}", note=[Accessed: 29-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_functioncompile, organization={Wolfram Research}, title={FunctionCompile}, year={2021}, url={https://reference.wolfram.com/language/ref/FunctionCompile.html}, note=[Accessed: 29-March-2025
]}