FunctionDeclaration

FunctionDeclaration[name,typedfun]

declares name to be a typed function suitable for use in a compiler environment.

Details

Examples

open allclose all

Basic Examples  (7)

FunctionDeclaration can be used in FunctionCompile:

The compilation uses the function declaration:

FunctionDeclaration can be used to add a definition to the default compiler environment:

Use the declaration in a compilation:

The compilation uses the function declaration:

Reset the compiler environment to clear the declaration:

FunctionDeclaration can be used to add a definition to a new compiler environment:

Use the CompilerEnvironment option to specify the environment:

A function name can be used in more than one declaration:

Call one version:

Call the other version:

Reset the compiler environment to clear the declaration:

A function declaration can call another declaration:

Compilation uses both declarations:

A function declaration can work for more than one type:

The declaration works for an integer argument:

The same declaration works for a string:

A function declaration can use definitions that use := to set up DownValues for their declarations:

Give the function a name and a type and state that the implementation comes from the DownValues:

Now a function that uses this declaration is compiled:

What is neat is that this workflow is compatible with the uncompiled version:

Scope  (4)

Symbol Definitions  (4)

Function declarations that use a symbol declaration can work with several definitions:

Make a declaration and compile a function that uses it:

This calls the version that uses a condition:

This calls the version that uses a literal 2:

This calls the last catchall version:

Function declarations that use a symbol declaration can be declared more than once with different types:

Make two declarations with different types and compile functions that use each:

Call the integer case:

Call the real case:

Function declarations that use a symbol declaration can be declared with a polymorphic type:

Make two declarations with different types and compile functions that use each:

Call the integer case:

Call the real case:

Function declarations that use a symbol declaration can call themselves:

Make a declaration and compile a function that uses it:

Call the function:

Applications  (1)

Symbol Definitions  (1)

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

A FunctionDeclaration that refers to the symbol:

A function that uses the symbol:

Compiled code for the function:

A vector plot based on the data that was created:

The compiled version is much faster:

An animation of the output:

Possible Issues  (1)

Symbol Definitions  (1)

There must always be an unrestricted definition (a catchall) for symbol definitions:

Patterns with a head specification are not supported:

Compound settings are not supported:

Sequence patterns are not supported:

Optional patterns are not supported:

Alternative patterns are not supported:

Alternative patterns are not supported:

Held patterns are not supported:

Verbatim patterns are not supported:

All the branches of pattern definitions must have a consistent type:

Wolfram Research (2021), FunctionDeclaration, Wolfram Language function, https://reference.wolfram.com/language/ref/FunctionDeclaration.html.

Text

Wolfram Research (2021), FunctionDeclaration, Wolfram Language function, https://reference.wolfram.com/language/ref/FunctionDeclaration.html.

CMS

Wolfram Language. 2021. "FunctionDeclaration." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/FunctionDeclaration.html.

APA

Wolfram Language. (2021). FunctionDeclaration. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FunctionDeclaration.html

BibTeX

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

BibLaTeX

@online{reference.wolfram_2022_functiondeclaration, organization={Wolfram Research}, title={FunctionDeclaration}, year={2021}, url={https://reference.wolfram.com/language/ref/FunctionDeclaration.html}, note=[Accessed: 29-September-2022 ]}