Procedures

PROCEDURE and ENDPROCEDURE:

LINGO allows for blocks of code known as procedures. These blocks can be called from anywhere in a calc section by simply referring to the procedure's name.   Procedures can be useful for portions of calc sections that must be executed multiple times.  The PROCEDURE and ENDPROCEDURE statements are used to bracket a procedure's code.  The PROCEDURE statement must be followed immediately by a unique name that follows the normal naming conventions.  The name must then be followed by a colon.  You must also place an ENDPROCEDURE statement immediately after the last statement of your procedure.   Procedures form their own model section that must lie outside data, init and calc sections.   Unlike a calc section, procedures are not automatically executed when solving a model.  Instead, you must reference the procedure's name in a calc section to execute its code.

As an example, the following procedure was extracted from the sample model, PROCEDURE.LG4:

PROCEDURE PRINT_REPORT:

  @WRITE( '     Start: ');

  @WRITEFOR( DAY: @FORMAT( START, '6.0f'));

  @WRITE( @FORMAT( @SUM( DAY: COST*START), '8.0f'));

  @WRITE( @NEWLINE( 1));

  @WRITE( '   On Duty: ');

  @WRITEFOR( DAY: @FORMAT( NEED + EXCESS, '6.0f'));

  @WRITE( @NEWLINE( 1));

  @WRITE( '  Required: ');

  @WRITEFOR( DAY: @FORMAT( NEED, '6.0f'));

  @WRITE( @NEWLINE( 1));

  @WRITE( '    Excess: ');

  @WRITEFOR( DAY: @FORMAT( EXCESS, '6.0f'));

  @WRITE( @NEWLINE( 2));

ENDPROCEDURE

Model: PROCEDURE

This procedure prints a small report for a staffing model.  The procedure is called several times from the model's main calc section to print solution reports for several permutations of the original model.   Here is a code fragment from the models calc section that illustrates calling the procedure:

  @WRITE( 'Solution 1 - min cost:', @NEWLINE( 1));

  PRINT_REPORT;

The first line prints a header file for the report, while the second line generates the actual report by calling the PRINT_REPORT procedure above.  A sample of one of the reports follows:

              MON   TUE   WED   THU   FRI   SAT   SUN   TOTAL

Solution 1 - min cost:

    Start:      5     4     0     6     2     2     4    4600

  On Duty:     19    17    15    19    17    14    14

 Required:     19    17    15    19    17    14    12

   Excess:      0     0     0     0     0     0     2

You may wish to open PROCECURE.LG4 in LINGO to experiment with the use of procedure calls.

You may also pass one, or more, scalar arguments to procedures.  For example, the following illustrates passing a variable to a procedure that prints out the value of the variable and its square root:

MODEL:

 

SETS:

 S1: X;

ENDSETS

 

PROCEDURE PRINT_ROOT( Z):

  @WRITE( 'The square root of ', Z, ' is: ', @SQRT( Z));

  @WRITE( @NEWLINE( 1));

ENDPROCEDURE

 

DATA:

  X = 1 4 9;

ENDDATA

 

CALC:
 @FOR( S1( I):

    PRINT_ROOT( X( I));

 );

 @WRITE( @NEWLINE( 1));

ENDCALC

 

END

If you run this model, you should see the output:

 

The square root of 1 is: 1

The square root of 4 is: 2

The square root of 9 is: 3