define procedure / function

Prev Next

Function Names

define procedure, define function, define procedure and function

Synopsis

define ... ( expression, parameters ... ) { statement(s); }

Description

define procedure defines a user procedure to be called as statements and without providing return values.
define function defines a user function which provides a return value and must be called inside an expression.
define procedure and function defines functions which can be called as procedures and functions as well.

All procedures and functions can be declared with fixed or variable user-definable number of parameters.

User functions can be defined anywhere in the code, but as a good practice, do this outside the main block. Inside the block function definition will only take place if the code runs (and not skips) over it. You may want to use an if() clause to define a function function name() either one or the other way, but be aware that presence of the function will be checked regardless if running or skipping over it.

Call as: procedure

Restrictions

Indirect parameter passing is disabled
Unlike other flow control functions, subsequent statements must always be encapsulated in a block using braces: { statement(s); }
The subsequent code block assumes local variable scope related to this procedure / function whenever new variables are created. Local variables will be discarded after returning to the caller.

Parameter count

1-5

Parameters

No.TypeDescription
1
input
string New Name

Give your procedure or function a new name. Then name may consist of multpile words with spaces, e.g. my sort function.

Opt. 2
input
set Parameter Declaration

This 2nd parameter must be specified if the new procedure or function comes with parameters to pass through. It must be a 2-level nested set where each such set declares one function parameter. The syntax is as follows:
{ { parameter name 1, parameter type 1, parameter direction 1 }, { parameter name 2, ... }, ... }
Zero parameters may also be specified as 2 nested empty sets. { { } }

. parameter name must be a string which uses this name to it as a locally accessible variable. Not recommended to add '[]' at the end of the name here because it would expect a pre-defined variable to be used as parameter name.
parameter type must be a string which declares the type(s) allowed. Both single types (e.g. numeral) as well as combined types are supported. See list of parameter types in user functions for all valid types and ther functionality.

parameter direction: One of the following:

Parameter direction Explanation
input Input parameter which may be any expression: Value, variable, table or a calculation. Changing the value inside the user procedure / function code is allowed and has no impact outside the procedure / function.
output Output parameter, must be a variable or table reference. Nonexisting variables will be initialized automatically. The final value assigned to the variable will be written back to the variable specified as parameter, or table entry. In the latter case, the value will be converted to string automatically.
io I/O parameter, must be a variable or table reference. Variables specified must exist before the function call is made. The final value assigned to the variable will be written back to the variable specified as parameter, or table entry. In the latter case, the value will be converted to string automatically.
reference Reference to variable. In this case, the code in the defined procedure or function gets the reference to the variable so the base variable and all member variables are visible and can be modified.

Opt. 3
input
numeral Minimum Parameter Count

Rules the minimum required number of parameters to be passed. Passing the remaining parameters is optional

Default value: According to number of parameter names specified
Opt. 4
input
numeral Maximum Parameter Count

Rules the maximum required number of parameters to be passed. The maximum number may be higher than the number of parameter names defined. Specify -1 if no upper limit applies.

Default value: According to number of parameter names specified
Opt. 5
input
numeral Repetition Count

The Repetition Count specifies the number of additional parameters to be specified if variable parameter count is enabled. 1 specifes any number is allowed between minimum and maximum number of parameters. And if the parameter lies outside range of defiend parameter types, then the last type appeis. 2 specifies that 2 additional parameters need to be added, and if the parameter lies outside the range of defined parameter types, then the last two parameter types apply sequentially. Similar rules apply to larger numbers specified.

Default value: 1

Exceptions

Procedure or function name is already defiend

Examples

               define function( my private function, { { guest name, string }, { vegetarian, boolean } }, 2, -1, 2 )
               {
                       echo( "Name of function: ", function name[] );
                       echo( "First invited guest: ", guest name[], select if( vegetarian[], " (vegetarian menu)", " (fish menu)" ) );

                       for (i[] = 3, i[] <= parameter count[], i[]+=2 ) // Additional guests
                       {
                               name[] = ("parameter " + str(i[]  ))[];
                               vegi[] = ("parameter " + str(i[]+1))[];

                               echo( "And: ", name[], select if( vegi[], " (vegetarian menu)", " (fish menu)" ) );
                       }


                       return( parameter count[] / 2 ); // Number of guests
               }


               guest count[] = my private function( Jim Bean, true );
               echo(new line, "Number of guests invited: ", guest count[], new line);

               guest count[] = my private function( Jim Bean, true, Tina Turner, false, Tom Cruise, false, Amsel Adams, true );
               echo(new line, "Number of guests invited to bigger party: ", guest count[]);

Output

Name of function: my private function
First invited guest: Jim Bean (vegetarian menu)

Number of guests invited: 1

Name of function: my private function
First invited guest: Jim Bean (vegetarian menu)
And: Tina Turner (fish menu)
And: Tom Cruise (fish menu)
And: Amsel Adams (vegetarian menu)

Number of guests invited to bigger party: 4
Try it yourself: Open LIB_Function_define_procedure.b4p in B4P_Examples.zip. Decompress before use.

See also

define additional procedure
define additional function
define additional procedure and function