Documentation

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

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

Square a number

Multiple two values

Calculate n to the p power (with default parameters)

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

Multiply values by x

Calculate speed

Define functions with scoped variables

To create custom functions with variables scoped to the function,

  1. Enclose your function body in a block ({}).
  2. Use a return statement to return a specific variable.
functionName = (param) => {
    exampleVar = "foo"

    return exampleVar
}

Example functions with scoped variables

Return an alert level based on a value

Convert a HEX color code to a name


Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

State of the InfluxDB Cloud Serverless documentation

InfluxDB Cloud Serverless documentation is a work in progress.

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.