({{, (RefLink[FunctionCompile, paclet:ref/FunctionCompile])[func] 
generates a compiled code function from a Wolfram Language function.}})

OpenersNotesSection(Spacer1)Details and Options

The function func is typically specified as a (RefLink[Function, paclet:ref/Function]) pure function. The variables in the (RefLink[Function, paclet:ref/Function]) object are typically annotated with (RefLink[Typed, paclet:ref/Typed]).

(RefLink[FunctionCompile, paclet:ref/FunctionCompile])[func] produces a (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) object that can be applied to suitable arguments just like func.

Inside func, (RefLink[KernelFunction, paclet:ref/KernelFunction]) can be used to indicate functions that should directly use the Wolfram Engine rather than being compiled into low-level code.

The code in the (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) object can be output for external purposes using (RefLink[FunctionCompileExport, paclet:ref/FunctionCompileExport]) and related functions.

(RefLink[FunctionCompile, paclet:ref/FunctionCompile]) has the option (RefLink[CompilerOptions, paclet:ref/CompilerOptions]), which allows detailed options to be passed to different parts of the compilation pipeline.

OpenersPrimaryExamplesSection(Spacer1)Examples  (14)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Compile a (RefLink[Function, paclet:ref/Function]) into a compiled code function:

cf=FunctionCompile[Function[Typed[arg,"MachineInteger"],arg+1]]

The (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) evaluates with an argument of the correct type:

cf[20]

If unexpected arguments are given, an error results:

cf[True, 1.5]

Many common operations are supported in compiled code:

cf=FunctionCompile[Function[Typed[len,"MachineInteger"],Table[i,{i,len}]]]

cf[20]

cf=FunctionCompile[Function[Typed[n,"MachineInteger"],Nest[Sin,10.1, n]]]

cf[ 200]

(RefLink[Information, paclet:ref/Information]) can extract the signature of the (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]):

cf=FunctionCompile[Function[Typed[arg,"MachineInteger"],arg+1]];
Information[cf,"Signature"]

OpenersExampleSection1(Spacer1)Scope  (9)

A wide range of native types are supported:

cf=FunctionCompile[Function[Typed[arg,"Boolean"],If[arg,1,2]]];
cf[False]

cf=FunctionCompile[Function[Typed[arg,"UnsignedInteger8"],N[arg]]];
cf[20]

cf=FunctionCompile[Function[Typed[arg,"Integer16"],10+ arg]];
cf[20]

cf=FunctionCompile[Function[Typed[arg,"Real64"],arg+2]];
cf[20.2]

cf=FunctionCompile[Function[Typed[arg,"Complex128"],arg+2]];
cf[20.2+3.4I]

Compound types are also supported:

cf=FunctionCompile[Function[Typed[arg,TypeSpecifier["PackedArray"]["MachineInteger",1]],
		Fold[Plus,arg]]];
cf[{4,5,6,1,2,6,3}]

cf=FunctionCompile[Function[Typed[arg,TypeSpecifier["NumericArray"]["Integer16",2]],
		Max[arg]]];
cf[NumericArray[{{4,5,6},{1,2,3}}, "Integer16"]]

Function types are also supported:

cf1=FunctionCompile[Function[Typed[arg,"MachineInteger"],arg+1]];

cf2=FunctionCompile[Function[{Typed[fun,{"MachineInteger"}->"MachineInteger"],Typed[arg,"MachineInteger"]},fun[arg]]];

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

cf2[cf1, 20]

The Wolfram Engine can be called using (RefLink[KernelFunction, paclet:ref/KernelFunction]). This defines a function in the Wolfram Engine:

f[x_]:=x^2

Use (RefLink[KernelFunction, paclet:ref/KernelFunction]) to call the user defined function from compiled code:

cf=FunctionCompile[Function[Typed[arg,"MachineInteger"],Typed[KernelFunction[f],{"MachineInteger"}->"MachineInteger"][arg]]]

cf[10]

Functions can be nested inside functions:

cf=FunctionCompile[Function[Typed[arg,"MachineInteger"], 
	Module[{f = #+1&}, 
		f[arg]]]];
cf[10]

Functions can be passed around as data:

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:

cf[True, 20]

cf[False, 20]

Nested functions can refer to variables in an outer scope:

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:

cf=FunctionCompile[Function[{Typed[arg1,"MachineInteger"]},
		Module[{f1=Function[{},arg1], f2=Function[f, f[]]},
	f2[f1]]]]

cf[ 10]

(RefLink[Print, paclet:ref/Print]) can be used to see the inside of a computation:

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

A (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) can be passed as arguments to other (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) expressions.

Define a function:

cf1=FunctionCompile[Function[{Typed[arg, "MachineInteger"]},If[EvenQ[arg], arg+1,arg-1]]]

cf1[10]

This function takes a function as an argument:

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:

cf2[cf1, 100]

OpenersExampleSection3(Spacer1)Possible Issues  (3)

If an error takes place while computing a function with the low-level code, the computation is run in the Wolfram Engine:

cf=FunctionCompile[Function[Typed[arg,"Real64"],Sqrt[arg]]]

There is no error here:

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:

cf[-10.2]

Integer constants are typed as the native machine integer:

cf=FunctionCompile[Function[{Typed[arg,"Integer8"]},arg+10]]

Use (RefLink[Typed, paclet:ref/Typed]) to specify a particular type:

cf=FunctionCompile[Function[{Typed[arg,"Integer8"]},arg+Typed[10,"Integer8"]]]

Returning a function from (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) is not supported:

FunctionCompile[Function[{},Function[Typed[arg,"MachineInteger"],arg+1]]]

Functions nested inside a (RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction]) can return functions:

cf=FunctionCompile[Function[{},Module[{f=(Function[Typed[arg,"MachineInteger"],arg+1])&},f[][20]]]]

cf[]

OpenersExampleSection4(Spacer1)Neat Examples  (1)

Nested functions can be used to implement recursive definitions:

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

cf[10]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[CompiledCodeFunction, paclet:ref/CompiledCodeFunction])  ▪  (RefLink[Function, paclet:ref/Function])  ▪  (RefLink[Typed, paclet:ref/Typed])  ▪  (RefLink[KernelFunction, paclet:ref/KernelFunction])  ▪  (RefLink[FunctionCompileExport, paclet:ref/FunctionCompileExport])  ▪  (RefLink[FunctionCompileExportLibrary, paclet:ref/FunctionCompileExportLibrary])  ▪  (RefLink[FunctionCompileExportString, paclet:ref/FunctionCompileExportString])  ▪  (RefLink[FunctionCompileExportByteArray, paclet:ref/FunctionCompileExportByteArray])  ▪  (RefLink[Compile, paclet:ref/Compile])  ▪  (RefLink[ExternalFunction, paclet:ref/ExternalFunction])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[External Language Interfaces, paclet:guide/ExternalLanguageInterfaces])}, {▪(RefLinkPlain[Code Compilation, paclet:guide/CodeCompilation])}, {▪(RefLinkPlain[Compiled Types, paclet:guide/CompiledTypes])}})}})