xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

PROCEDURE

Declares a procedure along with its formal parameters.

Syntax

[STATIC] [UTILITY] PROCEDURE <procName>( [<params,...>] )
           [FIELD <fieldName,...> [IN <aliasName>]]
          [MEMVAR <var_Dynamic,...>]
           [LOCAL <var_Local>  [:= <expression>] ,... ]
          [STATIC <var_Static> [:= <expression>] ,... ]

            <Statements>

           RETURN

Arguments

PROCEDURE <procName>( [<params,...>] )
This is the symbolic name of the declared procedure. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

Optionally, the names of parameters <params,...> accepted by the procedure can be specified as a comma separated list. These parameters have LOCAL scope within the procedure.

When the procedure is declared as STATIC PROCEDURE, it is only visible within the PRG file that contains the procedure declaration and cannot be invoked from elsewhere.

UTILITY
The UTILITY attribute indicates that the declared procedure should not be used as startup code even if it is the first declared procedure in the PRG source file.
FIELD <fieldName>
An optional list of field variables to use within the PROCEDURE can be declared with the FIELD statement.
MEMVAR <var_Dynamic>
If dynamic memory variables, i.e. PRIVATE or PUBLIC variables, are used in the procedure, they are declared with the MEMVAR statement.
LOCAL <var_Local> [:= <expression>]
Local variables are declared and optionally initialized using the LOCAL statement.
STATIC <var_Static> [:= <expression>]
Static variables are declared and optionally initialized using the STATIC statement.
RETURN
The RETURN statement terminates a procedure and branches control back to the calling routine. A procedure has no return value.

Description

The PROCEDURE statement declares a procedure along with an optional list of parameters accepted by the function. Statements programmed in the procedure body form a self-contained part of a program that is executed when a procedure is called. Thus, tasks of a program can be split into several procedures, each of which performs a sub-task when invoked.

The body of a procedure ends with the next FUNCTION, PROCEDURE or CLASS declaration, or at the end of file, which implies that procedure declarations cannot be nested.

The execution of a procedure ends when a RETURN statement is encountered in the procedure body. A procedure does not return a value to the calling routine, only functions have a return value. The RETURN value is the only difference between functions and procedures.

When a procedure is declared with the STATIC modifier, its visibility is restricted to the PRG file that contains the STATIC PROCEDURE declaration. The names of STATIC procedures are resolved by the compiler and do not exist at runtime of a program. The names of non-STATIC procedures, also referred to as public procedures, are resolved by the linker and do exist at runtime. Thus, public procedures can be accessed by the Macro operator (&) while STATIC procedures cannot.

It is possible to declare STATIC procedures with the same symbolic name in different PRG files. A name conflict to a public procedure with the same name declared in another PRG file does not arise. However, the symbolic names of public functions, procedures or classes must always be unique.

When a procedure is invoked with values being passed to it, they are assigned to the formal parameters declared with <params,...>. All variables declared in this list are LOCAL variables and their visibility is restricted to the statements programmed in the procedure body.

The number of values passed to a procedure does not need to match the number of parameters declared. When fewer values are passed, the corresponding parameters are initialized with NIL. When more values are passed, the additional values are not asssigned to parameters but can be retrieved using function HB_AParams().

Info

See also:FIELD, FUNCTION, HB_AParams(), LOCAL, MEMVAR, METHOD (declaration), PARAMETERS, PCount(), PRIVATE, PUBLIC, RETURN, STATIC
Category: Declaration , Statements

Example

// The PROCEDURE statement is used in may other examples of
// this documentation. See there.

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe