Function Parameter Directions

Prev Next

Introduction

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

Input Parameters

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
Try it yourself: Open LAN_Features_Function_parameter_directions.b4p in B4P_Examples.zip. Decompress before use.

Output Parameters

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 parameter set
3
Try it yourself: Open LAN_Features_Function_parameter_directions_01.b4p in B4P_Examples.zip. Decompress before use.

Input / Output Parameters

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
Try it yourself: Open LAN_Features_Function_parameter_directions_02.b4p in B4P_Examples.zip. Decompress before use.

References

Some functions take over the reference to the variables provided in the function parameters. References provide following benefits:

  • Higher performance (no payload data, e.g. big parameter sets, are copied in before the call and copied back after the call)
  • Direct access to the variable contents, especially applicable in user-defined procedures and user-defined functions
  • Access to member variables (structure and array members and their sub-members if available)
  • Access to variable properties, e.g. variable protection settings.

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
Try it yourself: Open LAN_Features_Function_parameter_directions_03.b4p in B4P_Examples.zip. Decompress before use.

Code Pieces

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.