define additional procedure / function

Prev Next

Function Names

define additional procedure, define additional function, define additional procedure and function

Synopsis

define additional ... ( expression, parameters ... );

Description

You want to define an additional function which is very similar to a custom function defined before, i.e. most of the code for the implementation is common and you do not want to repeat that code. The idea is to share the code among multiple user-defined functions rather than duplicating the code or doing sophisticated things like creating nested functions.

You are free to

  • Use different number of parameters
  • Use different parameter names
  • Use different types for the parameters
  • The additional function may be a procedure, function or both, regardless how the original user-defined function has been declared.
  • The code distinguishes among the function names used with following automatically generated local variable: function name [].

This function defined here expects no subsequent code because the code is provided in the primary function definition. Deleting and redefining the first user-defined function will not have an impact on the additional functions defined.

The three procedure names (e.g. define additional function defined here are closely related to their counterparts define function, with following exception: The original procedure or function name must be provided in the 2nd parameter, and no additional code related to this definition follows afterwards.

Call as: procedure

Restrictions

Indirect parameter passing is disabled
No subsequent code is expected for these procedures

Parameter count

2-6

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.

2
input
string Existing Name

Specify the name of an existing procedure or function to refer to.

Opt. 3
input
parameter set Parameter Declaration

Same rules as for the 2nd parameter in define procedure,define function ,define procedure and function .

Opt. 4
input
numeral Minimum Parameter Count

Same rules as for the 3rd parameter in define procedure,define function ,define procedure and function .

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

Same rules as for the 4th parameter in define procedure,define function ,define procedure and function .

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

Same rules as for the 5th parameter in define procedure,define function ,define procedure and function .

Default value: 1

Exceptions

Procedure or function name is already defined
Original procedure or function not found

Examples

      define function( private function, { { guest name, string }, { vegetarian, boolean } }, 2, -1, 2 )
      {
          echo( "Name of function: ", function name[] );
          main menu[] = select if( function name[] == bbq party, " (beef menu)", " (fish menu)" );
          echo( "First invited guest: ", guest name[], select if( vegetarian[], " (vegetarian menu)", main 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)", main menu[] ) );
          }

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

      define additional function( bbq party, private function, { { guest name, string }, { vegetarian, boolean } }, 2, -1, 2 );
      define additional function( fish for 2, private function, { { guest name, string }, { vegetarian, boolean } }, 2, 2, 0 );

      guest count[] = fish for 2( Susanne Ellen, false );
      echo(new line, "Number of guests invited: ", guest count[], new line);

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

Output

Name of function: fish for 2
First invited guest: Susanne Ellen (fish menu)

Number of guests invited: 1

Name of function: bbq party
First invited guest: Jim Bean (vegetarian menu)
And: Tina Turner (beef menu)
And: Tom Cruise (beef menu)
And: Amsel Adams (vegetarian menu)

Number of guests invited bbq: 4
Try it yourself: Open LIB_Function_define_additional_procedure.b4p in B4P_Examples.zip. Decompress before use.

See also

define procedure
define function
define procedure and function