define procedure, define function, define procedure and function
define ... ( expression, parameters ... ) { statement(s); }
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.
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.
1-5
No. | Type | Description | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
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:
| ||||||||||
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 |
Procedure or function name is already defiend
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[]);
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
define additional procedure
define additional function
define additional procedure and function