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
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.