B4P distinguishes among following function parameter directions which are described next.
Direction | Constants | Expressions | Variables | Table references |
---|---|---|---|---|
Input parameters | allowed | allowed | allowed (must be defined) | allowed |
Output parameters | not allowed | not allowed | allowed (may be defined) | allowed |
Input / output parameters | not allowed | not allowed | allowed (must be defined) | allowed |
References | not allowed | not allowed | allowed (must be defined) | not allowed |
Code pieces | See section on code pieces |
For input parameters, right-hand side expressions are expected, for exmpple values, variables, table references, calculations, etc. All variables used in these input parameters must be existing, i.e. values assigned.
a[] = World;
echo( "Hello ", a[], ' ', abs(-1)+2+3 );
Hello World 6
For output parameters, left-hand side expressions are expected in order to specify a destination location in a table or variable. Constants and calculated expressions are not allowed. Before the function is called, variables which are not yet existing, will be defined automatically. After the function call has been completed, the value will be written back to the variable or table location.
a[] = find( 'Say Hi!', { Ha, Hä, He, Hi, Ho }, 0, which[] );
echo( which[] ); // 3, because Hi is in position 3 in the set
3
For output parameters, left-hand side expressions are expected in order to specify a destination location in a table or variable. Constants and calculated expressions are not allowed. Specified variables must be existing. Before the function is called, the specified parameter will be applied to retrieve the input value. After the call has been completed, the value will be written back to the variable or table location.
table initialize( t, {{ Hi, He }} );
a[] = Ho;
exchange( [t:0,0], a[] );
echo( [t:..,0], " and ", a[] );
{'Ho','He'} and Hi
Some functions take over the reference to the variables provided in the function parameters. References provide following benefits:
The following code example shows how the user-defined procedure my funct accesses a member variable and defines an additional member variable.
define procedure( my func, { { a, all, reference } } )
{
echo(a[one]);
a[two] = TWO;
}
b[one] = ONE;
my func( b[] );
echo(b[two]);
ONE
TWO
Passing code pieces as function parameters is a unique feature in B4P. Code pieces can either be provided directly or as a string.
When the function is called, the code piece is checked for correct syntax. While the function is running, the code pieces provided may be
called multiple times, or in specific cases not at all.
See section on code pieces for further details.