# Function Building and Compilation (build_function)

At any time, callable functions can be generated from ModelingToolkit IR by using `ModelingToolkit.toexpr`

. This performs some cleaning to return an expression without extraneous pieces that commonly matches expressions one would write in functions like those for differential equation solvers and optimization libraries. These functions can be automatically parallelize and specialize on Julia types like static arrays and sparse matrices.

The core compilation process of ModelingToolkit IR is `build_function`

. `build_function`

takes an operation or an `AbstractArray`

of operations and generates a compilable version of the model for numerical solvers. The form of this output is dependent on the `target`

. By default, the target outputs Julia code, but other formats, such as C, Stan, and MATLAB are available. These can be generated as expressions which can then be evaluated into a callable function, or the compilers for the respective targets can be invoked to directly give back the function handle.

## build_function

`ModelingToolkit.build_function`

— Function`build_function`

Generates a numerically-usable function from a ModelingToolkit `Num`

.

```
build_function(ex, args...;
expression = Val{true},
target = JuliaTarget(),
kwargs...)
```

Arguments:

`ex`

: The`Num`

to compile`args`

: The arguments of the function`expression`

: Whether to generate code or whether to generate the compiled form. By default,`expression = Val{true}`

, which means that the code for the function is returned. If`Val{false}`

, then the returned value is compiled.

Keyword Arguments:

`target`

: The output target of the compilation process. Possible options are:`JuliaTarget`

: Generates a Julia function`CTarget`

: Generates a C function`StanTarget`

: Generates a function for compiling with the Stan probabilistic programming language`MATLABTarget`

: Generates an anonymous function for use in MATLAB and Octave environments

`fname`

: Used by some targets for the name of the function in the target space.

Note that not all build targets support the full compilation interface. Check the individual target documentation for details.

## Target-Specific Definitions

Missing docstring for `_build_function(target::JuliaTarget,args...;kwargs...)`

. Check Documenter's build log for details.

Missing docstring for `_build_function(target::CTarget,args...;kwargs...)`

. Check Documenter's build log for details.

Missing docstring for `_build_function(target::StanTarget,args...;kwargs...)`

. Check Documenter's build log for details.

Missing docstring for `_build_function(target::MATLABTarget,args...;kwargs...)`

. Check Documenter's build log for details.