Statement PROCEDURE Foundation

Declares procedures with a name, formal parameter list and variables.

[STATIC] PROCEDURE <ProcedureName> [( <Parameters,...>  )]
            [LOCAL <VarName_L> [ [:= <Expression>],...]]
           [STATIC <VarName_S> [ [:= <Expression>],...]]
           [MEMVAR <VarName_M,...>]
            [FIELD <FieldName,...> [IN <AliasName>]]
The STATIC clause limits the visibility of a procedure to the PRG file in which the procedure was declared. When the procedure is called from another PRG file, a link error occurs.
<ProcedureName> designates the name of a procedure. A procedure name must consist of alphanumeric characters. The underscore (_) is permitted as a special character, and the first character may not be numeric. The first 255 characters of procedure names, function names and variables are significant.
<Parameters,...> is a comma separated list which contains the names of the formal parameters that belong to the LOCAL storage class. They are assigned values from the calling function or procedure.
With the LOCAL declaration, a list of variables can be declared and initialized. Their visibility and lifetime is limited to the actual procedure (see LOCAL declaration).
<Expression> is any legal expression whose value is assigned to the corresponding variable (Initialization) using the inline assignment operator(:=).
With the STATIC declaration, a list of variables may be declared which belong to the storage class STATIC. Their visibility is limited to the actual procedure, but they remain and retain their value after the procedure ends (see STATIC declaration).
The MEMVAR declaration declares a list of variables which belong to the storage class PRIVATE or PUBLIC. They must be subsequently created and/or initialized with the statement PRIVATE or PUBLIC (see the statements PRIVATE and PUBLIC).
The FIELD declaration optionally declares a list of variables which belong to the storage class FIELD (see the FIELD statement). If the IN option is specified, unaliased references to the fields in the <AliasName> work area are treated as though preceded by the <AliasName> alias and only fields that exist in the work area <AliasName> are affected.
A procedure ends with the RETURN statement and control passes to the calling function or procedure. A procedure should contain at least one RETURN statement, but can be ended at any place with RETURN. If there is no RETURN statement, a procedure ends when a new PROCEDURE, FUNCTION, CLASS or METHOD declaration or the end of file is encountered.

The PROCEDURE declaration declares a section of the program as a procedure. A procedure contains a routine which is always executed when <ProcedureName> is followed by parentheses in the body of the code. Within the parentheses, any legal expression can be used. The values of these expressions are passed to the procedure and assigned to the variables which were declared in the list of formal parameters <Parameters,...>. "Parameter" identifies a variable to which a value is assigned by the calling of the procedure. The passed value is called an argument. The value of a parameter is visible within the procedure as a LOCAL variable.

Procedures are used to structure a program and to organize the tasks of a program in components. The difference between functions and procedures is that functions have a defined return value. Procedures, on the other hand, always return the value NIL, meaning they have no return value. Procedures are introduced by the declaration PROCEDURE, functions by the declaration FUNCTION. Both delimit a section of code which is executed by specifying the name followed by parentheses. The end of a procedure occurs when a new PROCEDURE, FUNCTION, CLASS or METHOD declaration is encountered, or when the end of the source code file is reached. It is good programming practice to indicate the end of a procedure with the RETURN statement. RETURN ends a procedure and gives the control back to the calling function or procedure. Multiple RETURN statements can be used in a procedure, but it is good programming practice to use only one -- at the end of the procedure.

The visibility of procedures can be restricted with the addition of STATIC. A STATIC PROCEDURE can only be called within the PRG file in which it was declared.

Procedures use the same syntactical rules as the Xbase++ functions. They can be called with no arguments, several arguments or with all declared arguments. When individual arguments are not passed, the value of the corresponding parameter at the call of the procedure is NIL. Any parameter validation must be done within the body of a procedure. A procedure can also be called with an alias operator placed in front, causing a specific work area to be selected before execution of the procedure. By default, arguments are passed to a procedure by value. To pass an argument by reference, use the reference operator @ in front of the name of the applicable variable. This applies only to character, numeric, logical or date variable types. Arrays and objects are passed automatically by reference.

// Almost all of the examples of Xbase++ documentation contain 
// a compilable example program in which the PROCEDURE 
// declaration is used (see another example). 

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.