BEGIN-PROCEDURE

Function

Begins a procedure.

Syntax

BEGIN-PROCEDURE procedure_name [LOCAL|(arg1[, argi]...)]
END-PROCEDURE

Arguments

procedure_name

Procedure name. Not case-sensitive.

LOCAL

Defines that this is a local procedure.

arg1 [, argi]...

Arguments passed to or returned from the procedure. Can be string variables ($arg), numeric variables (#arg), or date variables ($arg). To return a value passed back to the calling DO, place a colon (:) before the variable name. Arguments in BEGIN-PROCEDURE and DO must match in number, order, and type.

Description

The procedure name must be unique. The name is referenced in DO. Procedures contain other commands and paragraphs (for example, SELECT, SQL, DOCUMENT).

By default, procedures are global. That is, variables or columns defined within a procedure are known and can be referenced outside the procedure.

A procedure is local when the word LOCAL appears after the procedure name or when the procedure is declared with arguments. That is, variables declared within the procedure are available only within the procedure, even when the same variable name is used elsewhere in the program. Queries defined in a local proceduresl have local database column variable names assigned that do not conflict with similarly named columns defined in queries in other procedures.

Production Reporting procedures can be called recursively. However, unlike C or Pascal, Production Reporting only maintains one copy of the local variables and they are persistent.

Arguments passed by DO to a procedure must match in number:

To reference or declare global variables from local procedures, add a leading underscore to the variable name, after the initial $, #, or &. (Example: #_amount)

Note:

All Production Reporting reserved variables, such as #sql-status and $sql-error, are global variables. Within local procedures, they must be referenced using the leading underscore: #_sql‑status or $_sql-error.

Examples

The following example shows a BEGIN-PROCEDURE MAIN, that also executes the procedure PRINT-LIST, for each row returned from the SELECT statement. No parameters are passed to PRINT-LIST.

begin-procedure main
  begin-select
    name
    address
    phone
      do print_list
    from custlist order by name
  end-select
end-procedure    ! main

In the following example, five arguments are passed to the CALC procedure:

do Calc (&tax, 'OH', &county_name, 12, #amount)
begin-procedure Calc(#rate, $state, $county, #months, :#answer)
  .
  .
  .
  let #answer = ...
end-procedure

In the preceding example the value for:#answer is returned to #amount in the DO command.

The following example references global variables:

begin-procedure print-it ($a, $b)
  print  $_deptname (+2,5,20) ! $deptname is
  print  $a         (,+1) ! declared outside
  print  $b         (,+1) ! this procedure
end-procedure

See Also

DO and END-PROCEDURE