Products
  • Wolfram|One

    The definitive Wolfram Language and notebook experience

  • Mathematica

    The original technical computing environment

  • System Modeler

    Multidomain modeling and simulation of complex systems

  • Compute ServicesUse with Mathematica and Wolfram|One
  • AI AccessUse with Mathematica and Wolfram|One
  • Finance Platform
  • Wolfram|Alpha Notebook Edition
  • Application Server
  • Enterprise Private Cloud
  • Wolfram Engine
  • Wolfram Player
  • Wolfram Cloud App
  • Wolfram Player App

More mobile apps

Wolfram|Alpha

  • Wolfram|Alpha Website
  • Wolfram|Alpha APIs

AI Products

  • Wolfram AI Ecosystem
  • Wolfram Foundation Tool
  • Wolfram Cloud MCP
  • Wolfram Local MCP
  • Agent One API
  • CAG Component APIs

Core Technologies of Wolfram Products

  • Wolfram Language
  • Computable Data
  • Wolfram Notebooks
  • Linguistic Understanding

Additional Deployment Options

  • Wolfram Cloud
  • Wolfram Web Engine
  • wolframscript
  • WSTPServer
  • Group & Organizational Licensing
  • All Products
Consulting & Solutions

We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

Wolfram Consulting

Data Science, Finance & Business

  • Artificial Intelligence
  • Data Science
  • Healthcare
  • Real Estate

Engineering

  • Civil Engineering
  • Automotive Engineering
  • Sustainable Energy

Science & Technology

  • AgTech
  • Biosciences
  • Environmental Science
  • Food Science
  • Quantum Computation
  • Social and Behavioral Sciences

More Wolfram Solutions

Technology for Education

  • Research Universities
  • Colleges & Teaching Universities
  • High Schools
  • Resources for Students

Education Initiatives

  • Next-Gen EdTech
  • Computer-Based Math

More Solutions for Education

  • Contact Us
Learning & Support

Get Started

  • Wolfram Language Introduction
  • Fast Intro for Programmers
  • Fast Intro for Math Students
  • Wolfram Language Documentation

More Learning

  • Highlighted Core Areas
  • Demonstrations
  • YouTube
  • Daily Study Groups
  • Wolfram Schools and Programs
  • Books

Grow Your Skills

  • Wolfram U

    Courses in computing, science, life and more

  • Community

    Learn, solve problems and share ideas.

  • Blog

    News, views and insights from Wolfram

  • Resources for

    Software Developers

Tech Support

  • Contact Us
  • Support FAQs
  • Support FAQs
  • Contact Us
Company
  • About Wolfram
  • Career Center
  • All Sites & Resources
  • Connect & Follow
  • Contact Us

Work with Us

  • Student Ambassador Initiative
  • Wolfram for Startups
  • Student Opportunities
  • Jobs Using Wolfram Language

Educational Programs for Adults

  • Summer School
  • Winter School

Educational Programs for Youth

  • Middle School Camp
  • High School Research Program
  • Computational Adventures

Read

  • Stephen Wolfram's Writings
  • Wolfram Blog
  • Wolfram Tech | Books
  • Wolfram Media
  • Complex Systems

Educational Resources

  • Wolfram MathWorld
  • Wolfram in STEM
  • Wolfram Challenges
  • Wolfram Problem Generator

Wolfram Initiatives

  • Wolfram Science
  • Wolfram Foundation
  • History of Mathematics Project

Events

  • Stephen Wolfram Livestreams
  • Online & In-Person Events
  • Contact Us
  • Connect & Follow
For AIs
  • Your Account
  • User Portal
  • Wolfram Cloud
  • Products
    • Wolfram|One
    • Mathematica
    • System Modeler
    • Compute ServicesUse with Mathematica and Wolfram|One
    • AI AccessUse with Mathematica and Wolfram|One
    • Finance Platform
    • Wolfram|Alpha Notebook Edition
    • Application Server
    • Enterprise Private Cloud
    • Wolfram Engine
    • Wolfram Player
    • Wolfram Cloud App
    • Wolfram Player App

    More mobile apps

    • Wolfram|Alpha
      • Wolfram|Alpha Website
      • Wolfram|Alpha APIs
    • AI Products
      • Wolfram AI Ecosystem
      • Wolfram Foundation Tool
      • Wolfram Cloud MCP
      • Wolfram Local MCP
      • Agent One API
      • CAG Component APIs
    • Core Technologies
      • Wolfram Language
      • Computable Data
      • Wolfram Notebooks
      • Linguistic Understanding
    • Additional Deployment Options
      • Wolfram Cloud
      • Wolfram Web Engine
      • wolframscript
      • WSTPServer
    • Group & Organizational Licensing
    • All Products
  • Consulting & Solutions
    • Wolfram Consulting

    Data Science, Finance & Business

    • Artificial Intelligence
    • Data Science
    • Healthcare
    • Real Estate

    Engineering

    • Civil Engineering
    • Automotive Engineering
    • Sustainable Energy

    Science & Technology

    • AgTech
    • Biosciences
    • Environmental Science
    • Food Science
    • Quantum Computation
    • Social and Behavioral Sciences

    More Wolfram Solutions

    Technology for Education

    • Research Universities
    • Colleges & Teaching Universities
    • High Schools
    • Resources for Students

    Education Initiatives

    • Next-Gen EdTech
    • Computer-Based Math

    More Solutions for Education

    • Contact Us
  • Learning & Support

    Get Started

    • Wolfram Language Introduction
    • Fast Intro for Programmers
    • Fast Intro for Math Students
    • Wolfram Language Documentation

    Grow Your Skills

    • Wolfram U

      Courses in computing, science, life and more

    • Community

      Learn, solve problems and share ideas.

    • Blog

      News, views and insights from Wolfram

    • Resources for

      Software Developers
    • Tech Support
      • Contact Us
      • Support FAQs
    • More Learning
      • Highlighted Core Areas
      • Demonstrations
      • YouTube
      • Daily Study Groups
      • Wolfram Schools and Programs
      • Books
    • Support FAQs
    • Contact Us
  • Company
    • About Wolfram
    • Career Center
    • All Sites & Resources
    • Connect & Follow
    • Contact Us

    Work with Us

    • Student Ambassador Initiative
    • Wolfram for Startups
    • Student Opportunities
    • Jobs Using Wolfram Language

    Educational Programs for Adults

    • Summer School
    • Winter School

    Educational Programs for Youth

    • Middle School Camp
    • High School Research Program
    • Computational Adventures

    Read

    • Stephen Wolfram's Writings
    • Wolfram Blog
    • Wolfram Tech | Books
    • Wolfram Media
    • Complex Systems
    • Educational Resources
      • Wolfram MathWorld
      • Wolfram in STEM
      • Wolfram Challenges
      • Wolfram Problem Generator
    • Wolfram Initiatives
      • Wolfram Science
      • Wolfram Foundation
      • History of Mathematics Project
    • Events
      • Stephen Wolfram Livestreams
      • Online & In-Person Events
    • Contact Us
    • Connect & Follow
  • For AIs
  • Wolfram Cloud
  • Your Account
  • User Portal
Wolfram Language & System Documentation Center
FunctionCompile
  • See Also
    • CompiledCodeFunction
    • Function
    • Typed
    • TypeOf
    • KernelEvaluate
    • KernelFunction
    • DownValuesFunction
    • $CompilerEnvironment
    • CreateCompilerEnvironment
    • FunctionDeclaration
    • TypeDeclaration
    • FunctionCompileExport
    • FunctionCompileExportLibrary
    • FunctionCompileExportString
    • FunctionCompileExportByteArray
    • TargetSystem
    • $TargetSystems
    • $SystemID
    • Compile
    • ExternalFunction
    • CompiledComponent
    • CompilerRuntimeErrorAction
  • Related Guides
    • C/C++ Language Interface
    • Calling External Programs
    • External Language Interfaces
    • Code Compilation
    • Compiled Types
    • GPU Computing
    • Data Structures
    • Tuning & Debugging
    • GPU Computing with NVIDIA
    • GPU Computing with Apple
  • Tech Notes
    • Wolfram Compiler Manual
    • See Also
      • CompiledCodeFunction
      • Function
      • Typed
      • TypeOf
      • KernelEvaluate
      • KernelFunction
      • DownValuesFunction
      • $CompilerEnvironment
      • CreateCompilerEnvironment
      • FunctionDeclaration
      • TypeDeclaration
      • FunctionCompileExport
      • FunctionCompileExportLibrary
      • FunctionCompileExportString
      • FunctionCompileExportByteArray
      • TargetSystem
      • $TargetSystems
      • $SystemID
      • Compile
      • ExternalFunction
      • CompiledComponent
      • CompilerRuntimeErrorAction
    • Related Guides
      • C/C++ Language Interface
      • Calling External Programs
      • External Language Interfaces
      • Code Compilation
      • Compiled Types
      • GPU Computing
      • Data Structures
      • Tuning & Debugging
      • GPU Computing with NVIDIA
      • GPU Computing with Apple
    • Tech Notes
      • Wolfram Compiler Manual

FunctionCompile[f]

generates a compiled code function from a pure function.

FunctionCompile[{f1,f2,…}]

generates a list of compiled code functions from a list of pure functions.

FunctionCompile[k1f1,k2f2,…]

generates an association of compiled code functions from an association of Wolfram Language functions.

FunctionCompile[defs,fspec]

uses the local auxiliary definitions defs.

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Options  
CompilerEnvironment  
CompilerRuntimeErrorAction  
ProgressReporting  
TargetSystem  
UseEmbeddedLibrary  
Properties & Relations  
Possible Issues  
Errors  
Creating Functions  
Neat Examples  
See Also
Tech Notes
Related Guides
History
Cite this Page
BUILT-IN SYMBOL
  • See Also
    • CompiledCodeFunction
    • Function
    • Typed
    • TypeOf
    • KernelEvaluate
    • KernelFunction
    • DownValuesFunction
    • $CompilerEnvironment
    • CreateCompilerEnvironment
    • FunctionDeclaration
    • TypeDeclaration
    • FunctionCompileExport
    • FunctionCompileExportLibrary
    • FunctionCompileExportString
    • FunctionCompileExportByteArray
    • TargetSystem
    • $TargetSystems
    • $SystemID
    • Compile
    • ExternalFunction
    • CompiledComponent
    • CompilerRuntimeErrorAction
  • Related Guides
    • C/C++ Language Interface
    • Calling External Programs
    • External Language Interfaces
    • Code Compilation
    • Compiled Types
    • GPU Computing
    • Data Structures
    • Tuning & Debugging
    • GPU Computing with NVIDIA
    • GPU Computing with Apple
  • Tech Notes
    • Wolfram Compiler Manual
    • See Also
      • CompiledCodeFunction
      • Function
      • Typed
      • TypeOf
      • KernelEvaluate
      • KernelFunction
      • DownValuesFunction
      • $CompilerEnvironment
      • CreateCompilerEnvironment
      • FunctionDeclaration
      • TypeDeclaration
      • FunctionCompileExport
      • FunctionCompileExportLibrary
      • FunctionCompileExportString
      • FunctionCompileExportByteArray
      • TargetSystem
      • $TargetSystems
      • $SystemID
      • Compile
      • ExternalFunction
      • CompiledComponent
      • CompilerRuntimeErrorAction
    • Related Guides
      • C/C++ Language Interface
      • Calling External Programs
      • External Language Interfaces
      • Code Compilation
      • Compiled Types
      • GPU Computing
      • Data Structures
      • Tuning & Debugging
      • GPU Computing with NVIDIA
      • GPU Computing with Apple
    • Tech Notes
      • Wolfram Compiler Manual

FunctionCompile

FunctionCompile[f]

generates a compiled code function from a pure function.

FunctionCompile[{f1,f2,…}]

generates a list of compiled code functions from a list of pure functions.

FunctionCompile[k1f1,k2f2,…]

generates an association of compiled code functions from an association of Wolfram Language functions.

FunctionCompile[defs,fspec]

uses the local auxiliary definitions defs.

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 Automatican environment of definitions for compilation
    CompilerOptionsAutomaticdetailed options for the compilation pipeline
    CompilerRuntimeErrorAction Automaticthe behavior when there is an unrecoverable error while executing low-level code
    ProgressReporting Automatichow to report progress during the compilation
    TargetSystem Inheritedmachine architectures for code generation
    UseEmbeddedLibrary Falsecreate and embed a shared library for this architecture
  • Possible settings for TargetSystem are:
  • Automaticgenerate additional code for key machine architectures
    Allgenerate additional code for all machine architectures
    trggenerate 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:
  • Automaticrerun the computation in the Wolfram Engine if it avoids compiler-specific code
    "Evaluate"always rerun the computation in the Wolfram Engine
    Nonereturn a failure object that represents the error
    funapply fun to the failure object and return the result

Examples

open all close all

Basic Examples  (6)

Compile a Function into a compiled code function:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]]

The CompiledCodeFunction evaluates with an argument of the correct type:

Wolfram Language code: cf[20]

If unexpected arguments are given, an error results:

Wolfram Language code: cf[True, 1.5]

Many common operations are supported in compiled code:

Wolfram Language code: cf = FunctionCompile[Function[Typed[len, "MachineInteger"], Table[i, {i, len}]]]
Wolfram Language code: cf[20]
Wolfram Language code: cf = FunctionCompile[Function[Typed[n, "MachineInteger"], Nest[Sin, 10.1, n]]]
Wolfram Language code: cf[ 200]

Information can extract the type of the CompiledCodeFunction:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]]; Information[cf, "Type"]

Several functions can be compiled with FunctionCompile:

Wolfram Language code: cfs = FunctionCompile[{Function[Typed[arg, "Integer64"], arg + 1], Function[Typed[arg, "Integer64"], arg - 1]}]
Wolfram Language code: cfs[[1]][10]

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

Wolfram Language code: cfs = FunctionCompile[<|"f1" -> Function[Typed[arg, "Integer64"], arg + 1], "f2" -> Function[Typed[arg, "Integer64"], arg - 1]|>]
Wolfram Language code: cfs[[1]][10]

Auxiliary definitions can be given with FunctionDeclaration:

Wolfram Language code: dec = FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]]; cf = FunctionCompile[dec, Function[Typed[arg, "Integer64"], AddTwo[AddTwo[arg]]]]
Wolfram Language code: cf[ 200]

New types can be added with TypeDeclaration:

Wolfram Language code: dec = TypeDeclaration["Product", "simpleProd", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>];

Compile a function that instantiates a product type and then extracts a field:

Wolfram Language code: cf = FunctionCompile[dec, Function[{}, With[{prod = CreateTypeInstance["simpleProd", <|"field1" -> 12, "field2" -> True|>]}, prod["field1"]] ]]
Wolfram Language code: cf[ ]

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

Wolfram Language code: dec = FunctionDeclaration[squareFun, Typed[{"Real64"} -> "Real64"]@Function[{arg}, arg ^ 2]];
Wolfram Language code: cf = FunctionCompile[dec, squareFun]
Wolfram Language code: cf[1.5]

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

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Real64"], Module[{size = arg < 10}, Sin[size]]]]

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

Wolfram Language code: [image]

Scope  (12)

A wide range of native types are supported:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Boolean"], If[arg, 1, 2]]]; cf[False]
Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "UnsignedInteger8"], N[arg]]]; cf[20]
Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Integer16"], 10 + arg]]; cf[20]
Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Real64"], arg + 2]]; cf[20.2]
Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Complex128"], arg + 2]]; cf[20.2 + 3.4I]

Compound types are also supported:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "PackedArray"::["MachineInteger", 1]], Fold[Plus, arg]]]; cf[{4, 5, 6, 1, 2, 6, 3}]
Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "NumericArray"::["Integer16", 2]], Max[arg]]]; cf[NumericArray[{{4, 5, 6}, {1, 2, 3}}, "Integer16"]]

Function types are also supported:

Wolfram Language code: cf1 = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]];
Wolfram Language code: cf2 = FunctionCompile[Function[{Typed[fun, {"MachineInteger"} -> "MachineInteger"], Typed[arg, "MachineInteger"]}, fun[arg]]];

Passing in the function and integer arguments carries out the required computation:

Wolfram Language code: cf2[cf1, 20]

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

Wolfram Language code: f[x_] := x ^ 2

Use KernelFunction to call the user-defined function from compiled code:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], Typed[KernelFunction[f], {"MachineInteger"} -> "MachineInteger"][arg]]]
Wolfram Language code: cf[10]

Functions can be passed around as data:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[cond, "Boolean"], Typed[arg, "MachineInteger"]}, Module[{f, g}, f = If[cond, # + 1&, # - 1&]; g = #1[#2]&; g[f, arg]]]];

The first argument selects which nested function is used:

Wolfram Language code: cf[True, 20]
Wolfram Language code: cf[False, 20]

Nested functions can refer to variables in an outer scope:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg1, "MachineInteger"]}, Module[{f = Function[{}, arg1]}, f[]]]]; cf[10]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg1, "MachineInteger"]}, Module[{f1 = Function[{}, arg1], f2 = Function[f, f[]]}, f2[f1]]]]
Wolfram Language code: cf[ 10]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg1, TypeSpecifier["PackedArray"]["MachineInteger", 1]]}, Map[(Print[#];# ^ 2)&, arg1]]]; cf[{5, 6, 2, 1, 6, 7}]

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

Define a function:

Wolfram Language code: cf1 = FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, If[EvenQ[arg], arg + 1, arg - 1]]]
Wolfram Language code: cf1[10]

This function takes a function as an argument:

Wolfram Language code: cf2 = FunctionCompile[Function[{Typed[fun, { "MachineInteger"} -> "MachineInteger"], Typed[lim, "MachineInteger"]}, Table[fun[i], {i, lim}]]]

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

Wolfram Language code: cf2[cf1, 100]

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

Wolfram Language code: fun1[x_] := Echo[x ^ 2]

The FunctionDeclaration gives the function a name and a type and states that the implementation comes from the DownValues:

Wolfram Language code: decl = FunctionDeclaration[fun1, Typed[ {"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[fun1]];

Now a function that uses this declaration is compiled:

Wolfram Language code: func = Function[{Typed[len, "Integer64"]}, Table[ fun1[i], {i, len}]]; cf = FunctionCompile[ decl, func];
Wolfram Language code: cf[5]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, arg]]; Information[cf, "LLVMBinary"]

Compilation results can be saved using Once:

Wolfram Language code: cf = Once[FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, arg + 2]], "Notebook"]; cf[10]

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

Wolfram Language code: cf = Once[FunctionCompile[ FunctionDeclaration[flowCompute, Typed[{"Real64", "Real64"} -> "PackedArray"::["Real64", 2]]@Function[{x, y}, Module[{z}, z = 1 - 1 / (x + I y) ^ 2; {{x, y}, {Re[z], -Im[z]}} ]]] , Function[{Typed[x0, "Real64"], Typed[x1, "Real64"], Typed[y0, "Real64"], Typed[y1, "Real64"]}, Table[Flatten[ Table[{flowCompute[x + d / 4, y], flowCompute[x + d / 4, -y]}, {x, x0, x1}, {y, y0, y1}], 2] , {d, 0, 3, 0.3}]] ], "Notebook"];
Wolfram Language code: ListVectorPlot[First[cf[-7, 5, 0.5, 3]]]

Options  (6)

CompilerEnvironment  (1)

Create a compiler environment and add a function definition:

Wolfram Language code: env = CreateCompilerEnvironment[]; CompilerEnvironmentAppendTo[env, FunctionDeclaration[addTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2arg]]];

Use a compiler environment in a compilation:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, addTwo[arg]], CompilerEnvironment -> env]
Wolfram Language code: cf[10]

CompilerRuntimeErrorAction  (1)

The behavior of errors when executing low-level code is governed by the CompilerRuntimeErrorAction:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> Automatic]

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

Wolfram Language code: cf[250, 250]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, Module[ {ptr = ToRawPointer[x + y]}, FromRawPointer[ptr]]], CompilerRuntimeErrorAction -> Automatic]; cf[250, 250]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, Module[ {ptr = ToRawPointer[x + y]}, FromRawPointer[ptr]]], CompilerRuntimeErrorAction -> "Evaluate"]; cf[250, 250]

A setting of None never reruns in the evaluator:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> None]; cf[250, 250]

A function can be given to obtain custom behavior:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> Framed]; cf[250, 250]

ProgressReporting  (1)

Progress during a compilation is reported:

Wolfram Language code: FunctionCompile[ Function[Typed[arg, "PackedArray"::["Real64", 1]], BinCounts[arg, 1.5]]]

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:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], TargetSystem -> {"Windows-x86-64"}]
Wolfram Language code: Information[cf, "LLVMBinary"]

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

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], TargetSystem -> All]
Wolfram Language code: Information[cf, "LLVMBinary"]

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:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], UseEmbeddedLibrary -> True]

The code function now includes an embedded library:

Wolfram Language code: cf[[1]]["EmbeddedLibraries"]

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

Wolfram Language code: cf>>comp.wl; cf1 = <<comp.wl

The restored code function works as expected:

Wolfram Language code: cf1[10]

Properties & Relations  (1)

TypeOf is a quick way to check if something may compile, because it just determines the type of its argument:

Wolfram Language code: TypeOf[Function[{Typed[x, "Real64"], Typed[y, "Integer64"]}, x + y]]

Possible Issues  (4)

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:

Wolfram Language code: cf = FunctionCompile[Function[Typed[arg, "Real64"], Sqrt[arg]]]

There is no error here:

Wolfram Language code: cf[10.5]

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:

Wolfram Language code: cf[-10.2]

Integer constants are typed as the native machine integer:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer8"]}, arg + 10]]

Use Typed to specify a particular type:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "Integer8"]}, arg + Typed[10, "Integer8"]]]

Returning a function from CompiledCodeFunction is not supported:

Wolfram Language code: FunctionCompile[Function[{}, Function[Typed[arg, "MachineInteger"], arg + 1]]]

Creating Functions  (1)

FunctionCompile cannot create a function if there is more than one declaration:

Wolfram Language code: dec1 = FunctionDeclaration[squareFun, Typed[{"Real64"} -> "Real64"]@Function[{arg}, arg ^ 2]]; dec2 = FunctionDeclaration[squareFun, Typed[{"Integer64"} -> "Integer64"]@Function[{arg}, arg ^ 2]];
Wolfram Language code: cf = FunctionCompile[{dec1, dec2}, squareFun]

If Typed is used the compilation can work:

Wolfram Language code: cf = FunctionCompile[{dec1, dec2}, Typed[squareFun, {"Real64"} -> "Real64"]]

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

Wolfram Language code: dec = FunctionDeclaration[squareFun, Typed[ForAllType[a, {a} -> a]]@Function[{arg}, arg ^ 2]];
Wolfram Language code: cf = FunctionCompile[dec, squareFun]

If Typed is used the compilation can work:

Wolfram Language code: cf = FunctionCompile[dec, Typed[squareFun, {"Real64"} -> "Real64"]]

Neat Examples  (2)

Nested functions can be used to implement recursive definitions:

Wolfram Language code: cf = FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, Module[{factFun}, factFun = Function[{arg1}, If[arg1 === 1, 1, arg1 * factFun[arg1 - 1]]]; factFun[arg]]]]
Wolfram Language code: cf[10]

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

Wolfram Language code: restrictFlow[ x_, y_] := Module[{z}, z = 1 - 1 / (x + I y) ^ 2; {{x, y}, {Re[z], -Im[z]}} ]

A FunctionDeclaration that refers to the symbol:

Wolfram Language code: decl = FunctionDeclaration[restrictFlow, Typed[{"Real64", "Real64"} -> "PackedArray"::["Real64", 2]]@DownValuesFunction[restrictFlow]];

A function that uses the symbol:

Wolfram Language code: func = Function[{Typed[x0, "Real64"], Typed[x1, "Real64"], Typed[y0, "Real64"], Typed[y1, "Real64"]}, Table[Flatten[ Table[{restrictFlow[x + d / 4, y], restrictFlow[x + d / 4, -y]}, {x, x0, x1}, {y, y0, y1}], 2] , {d, 0, 3, 0.3}]];

Compiled code for the function:

Wolfram Language code: cf = FunctionCompile[decl, func]
Wolfram Language code: data = cf[-7, 5, 0.5, 3];

A vector plot based on the data that was created:

Wolfram Language code: ListVectorPlot[First[data]]

The compiled version is much faster:

Wolfram Language code: func[-7, 5, 0.5, 3];//RepeatedTiming
Wolfram Language code: cf[-7, 5, 0.5, 3];//RepeatedTiming

An animation of the output:

Wolfram Language code: Animate[ListVectorPlot[ Part[data, i] ], {i, 1, Length[data], 1}, SaveDefinitions -> True, AnimationRunning -> False]

See Also

CompiledCodeFunction  Function  Typed  TypeOf  KernelEvaluate  KernelFunction  DownValuesFunction  $CompilerEnvironment  CreateCompilerEnvironment  FunctionDeclaration  TypeDeclaration  FunctionCompileExport  FunctionCompileExportLibrary  FunctionCompileExportString  FunctionCompileExportByteArray  TargetSystem  $TargetSystems  $SystemID  Compile  ExternalFunction  CompiledComponent  CompilerRuntimeErrorAction

Tech Notes

    ▪
  • Wolfram Compiler Manual

Related Guides

    ▪
  • C/C++ Language Interface
  • ▪
  • Calling External Programs
  • ▪
  • External Language Interfaces
  • ▪
  • Code Compilation
  • ▪
  • Compiled Types
  • ▪
  • GPU Computing
  • ▪
  • Data Structures
  • ▪
  • Tuning & Debugging
  • ▪
  • GPU Computing with NVIDIA
  • ▪
  • GPU Computing with Apple

History

Introduced in 2019 (12.0) | Updated in 2021 (12.3)

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).

CMS

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

BibTeX

@misc{reference.wolfram_2026_functioncompile, author="Wolfram Research", title="{FunctionCompile}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/FunctionCompile.html}", note=[Accessed: 01-September-2026]}

BibLaTeX

@online{reference.wolfram_2026_functioncompile, organization={Wolfram Research}, title={FunctionCompile}, year={2021}, url={https://reference.wolfram.com/language/ref/FunctionCompile.html}, note=[Accessed: 01-September-2026]}

Top
Introduction for Programmers
Introductory Book
Wolfram Function Repository | Wolfram Data Repository | Wolfram Data Drop | Wolfram Language Products
Top
  • Products
  • Wolfram|One
  • Mathematica
  • AI Access
  • Compute Services
  • System Modeler

  • Wolfram|Alpha Notebook Edition
  • Wolfram|Alpha Pro
  • Mobile Apps

  • Wolfram Engine
  • Wolfram Player

  • Volume & Site Licensing
  • Server Deployment Options
  • Consulting
  • Wolfram Consulting
  • Repositories
  • Data Repository
  • Function Repository
  • Community Paclet Repository
  • Neural Net Repository
  • Prompt Repository

  • Wolfram Language Example Repository
  • Notebook Archive
  • Wolfram GitHub
  • Learning
  • Wolfram U
  • Wolfram Language Documentation
  • Webinars & Training
  • Educational Programs

  • Wolfram Language Introduction
  • Fast Introduction for Programmers
  • Fast Introduction for Math Students
  • Books

  • Wolfram Community
  • Wolfram Blog
  • Public Resources
  • Wolfram|Alpha
  • Wolfram Problem Generator
  • Wolfram Challenges

  • Computer-Based Math
  • Computational Thinking
  • Computational Adventures

  • Demonstrations Project
  • Wolfram Data Drop
  • MathWorld
  • Wolfram Science
  • Wolfram Media Publishing
  • Customer Resources
  • Store
  • Product Downloads
  • User Portal
  • Your Account
  • Organization Access

  • Support FAQ
  • Contact Support
  • Company
  • About Wolfram
  • Careers
  • Contact
  • Events
Wolfram Community Wolfram Blog
Legal & Privacy Policy
WolframAlpha.com | WolframCloud.com
© 2026 Wolfram
© 2026 Wolfram | Legal & Privacy Policy |
English