Define custom functions
Flux’s functional syntax lets you define custom functions. Learn the basics of creating your own functions.
On this page:
- Function definition syntax
- Custom function examples
- Create a custom transformation
- Define functions with scoped variables
Function definition syntax
The basic syntax for defining functions in Flux is as follows:
// Basic function definition syntax
functionName = (functionParameters) => functionBody
- functionName: Name to use to execute the function.
- functionParameters: Comma-separated list of parameters passed into the function.
- functionBody: Operations on function parameters.
Define parameter defaults
Use the =
assignment operator to assign a default value to function parameters
in your function definition:
functionName = (param1=defaultVal1, param2=defaultVal2) => functionBody
Defaults are overridden by explicitly defining the parameter in the function call. Parameters without default values are considered required parameters.
Custom function examples
Create a custom transformation
A transformation is a function that takes a stream of tables as input, operates on the input, and then outputs a new stream of tables.
The pipe-forward operator (|>
)
pipes data from the previous identifier or function forward into a transformation.
To use piped-forward data, assign a function parameter to the
pipe-receive operator (<-
).
In the following example, the function x()
receives piped-forwarded data and assigns it to the t
parameter.
In the function body, t
is piped forward into other operations to generate output.
x = (t=<-) => t |> //...
Custom transformation examples
Define functions with scoped variables
To create custom functions with variables scoped to the function,
- Enclose your function body in a
block (
{}
). - Use a
return
statement to return a specific variable.
functionName = (param) => {
exampleVar = "foo"
return exampleVar
}
Example functions with scoped variables
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, use the following resources:
Customers with an annual or support contract can contact InfluxData Support.