Function blocks and scoped variables
This version of gnuplot introduces a mechanism for invoking a block of
standard gnuplot commands as a callable function. A function block can
accept from 0 to 9 parameters and returns a value. Function blocks can be
used to calculate and assign a new value to a variable, to combine with other
functions and operators, or to perform a repetitive task preparing data.
There are three components to this mechanism.
See local, scope, function blocks, return.
- The local qualifier allows optional declaration of a variable or array
whose scope is limited to the duration of execution of the program unit in which it is found. These units currently include execution of a load or call statement, function block evaluation, and the code block in curly brackets following an if, else, do for, or while statement. If the name of a local variable duplicates the name of a global variable, the global variable is shadowed until exit from the local scope.
- The function command declares a named function block (effectively an
array of strings) containing gnuplot commands. When the function block is invoked, commands are executed successively until the end of the block or until a return command is encountered.
- The return <expression> command terminates execution of a function block.
The result of evaluating <expression> is returned as the value of the function. Anywhere outside a function block return acts like exit.
For an example of using this mechanism to define and plot a non-trivial
function that is too complicated for a simple one-line definition f(x) = ...
please see
function_block.dem