Functions

As you write larger formulas, it becomes desirable to avoid code duplication and to have a way to better structure the formula code. To do this, you can group code in functions that you can call just like the built-in functions.

Here is a complete Mandelbrot formula that shows how to declare and use a function that calculates one iteration of the Mandelbrot set:

MandelbrotWithFunction {
init:
  complex func calculateMandelbrot(const complex x)
    return sqr(x) + #pixel
  endfunc

  z = 0
loop:
  z = calculateMandelbrot(z)
bailout:
  |z| < 4
}

Functions declared in a global section can write to global variables, but you cannot call these from outside the global section, unless the entire function is declared as const (in which case it can only read from any variables):

Test {
global:
  float x

  float func getXSquared() const
    return x * x
  endfunc

init:
  z = getXSquared()
  ...
} 

Next: Function arguments

See Also
Built-in functions
Methods