| **Navigation:**  [[introduction.htm|Language Reference]] > 2 - Program Source Code Format > Program Format >====== PROCEDURE (define a procedure) ====== | [[module specify member source file .htm|{{btn_prev_n.gif|Previous page}}]][[introduction.htm|{{btn_home_n.gif|Return to chapter overview}}]][[code begin executable statements .htm|{{btn_next_n.gif|Next page}}]] | | || | //label// | **PROCEDURE** [ **(**// parameter list //**) **] | | | //local data// | | | **CODE** | | | //statements// | | | [**RETURN(**// //[ //value //] **) **] | {{blk2blue.jpg|blk2blue.jpg}} | **PROCEDURE** | Begins a section of source code that can be executed from within a PROGRAM. | | //label// | Names the PROCEDURE. For a CLASS method's definition, this may contain the label of the CLASS prepended to the label of the PROCEDURE. | | //parameter list// | A comma delimited list of names (and, optionally, their data types) of the parameters passed to the PROCEDURE. These names define the local references within the PROCEDURE to the passed parameters. For a CLASS method's definition, this may contain the label of the CLASS (named SELF) as an implicit first parameter (if the class is not prepended to the PROCEDURE's //label//), and must always contain both the data type and parameter name. | | //local data// | Declare Local data visible only in this procedure. | | **CODE** | Terminate the data declaration section and begin the executable code section of the PROCEDURE. | | //statements// | Executable program instructions. | | **RETURN** | Terminate procedure execution. Return to the point from which the procedure was called and return the //value// to the expression in which the procedure was used (if the procedure has been prototyped to return a value). | | //value// | A numeric or string constant or variable which specifies the result of the procedure call. | **PROCEDURE** begins a section of source code that can be executed from within a PROGRAM. It is called by naming the PROCEDURE //label// (with its //parameter list//, if any) as an executable statement in the code section of a PROGRAM or PROCEDURE. The //parameter list// defines the data type of each parameter (optional) followed by the label of the parameter as used within the PROCEDURE's source code (required). Each parameter is separated by a comma. The data type of each parameter (including the angle brackets if the parameter is omittable) is required along with the parameter's label if the procedure is overloaded (has multiple definitions). The //parameter list// may be exactly the same as it appears in the PROCEDURE's prototype, if that prototype contains labels for the parameters. A PROCEDURE may contain one or more ROUTINEs in its executable code //statements//. A ROUTINE is a section of executable code local to the PROCEDURE which is called with the DO statement. A PROCEDURE terminates and returns to its caller when a RETURN statement executes. An implicit RETURN occurs at the end of the executable code. The end of executable code for the PROCEDURE is defined as the end of the source file, or the first encounter of a ROUTINE or another PROCEDURE. A RETURN statement is required if the PROCEDURE has been prototyped to return a //value//. A PROCEDURE which has been prototyped to return a //value //can be used as an expression component, or passed as a parameter to another PROCEDURE. A PROCEDURE which has been prototyped to return a //value //may also be called in the same manner as a PROCEDURE without a RETURN //value//, if the program logic does not require the RETURN //value//. In this case, if the PROCEDURE prototype does not have the PROC attribute, the compiler will generate a warning which may be safely ignored. Data declared within a PROCEDURE, between the keywords PROCEDURE and CODE, is Procedure Local data that can only be accessed by that PROCEDURE (unless passed as a parameter to another PROCEDURE). This data is allocated memory upon entering the procedure, and de-allocated when it terminates. If the data is smaller than the stack threshold (5K is the default) it is placed on the stack, otherwise it is allocated from the heap. A PROCEDURE must have a prototype declared in a CLASS or the MAP of a PROGRAM or MEMBER module. If declared in the PROGRAM MAP, it is available to any other procedure in the program. If declared in a MEMBER MAP, it is available to other procedures in that MEMBER module. **Example:** ** PROGRAM                         !Example program code** ** MAP** **OpenFile ****PROCEDURE****(FILE AnyFile)!Procedure prototype with parameter** **ShoTime   ****PROCEDURE****              !Procedure prototype without parameter** **DayString ****PROCEDURE****,STRING       !Procedure prototype with a return value** ** END ** **FileOne FILE,DRIVER('Clarion')   !Declare a file** **         RECORD                  !begin record declaration** **Name      STRING(20)** **Number    LONG** **         END                     ! end record declaration** **        END                      !End file declaration** **TodayString STRING(9)** ** CODE** ** TodayString = DayString()      !Procedure called with a return value** ** OpenFile(FileOne)              !Call procedure to open file** ** ShoTime                        !Call ShoTime procedure** **  !More executable statements** **OpenFile ****PROCEDURE****(FILE AnyFile)!Open any file** ** CODE                           !Begin code section** ** OPEN(AnyFile)                  !Open the file** ** IF ERRORCODE() = 2             !If file not found** **  CREATE(AnyFile)               !create it** ** END   ** ** RETURN                         !Return to caller** **ShoTime ****PROCEDURE****               !Show time** **Time  LONG                      !Local variable** **Window WINDOW,CENTER** **        STRING(@T3),USE(Time),AT(34,70)** **        BUTTON('Exit'),AT(138,92),USE(?Exit)** **       END** ** CODE                           !Begin executable code section** ** Time = CLOCK()                 !Get time from system** ** OPEN(Window)** ** ACCEPT** **  CASE ACCEPTED()** **  OF ?Exit** **   BREAK** **  END** ** END** ** RETURN                         !Return to caller** **DayString   ****PROCEDURE****           !Day string procedure** **ReturnString STRING(9),AUTO     !Uninitialized local stack variable** ** CODE                           !Begin executable code section** ** EXECUTE (TODAY() % 7) + 1      !Find day of week from system date** **  ReturnString = 'Sunday'** **  ReturnString = 'Monday'** **  ReturnString = 'Tuesday'** **  ReturnString = 'Wednesday'** **  ReturnString = 'Thursday'** **  ReturnString = 'Friday'** **  ReturnString = 'Saturday'** ** END** ** RETURN(ReturnString)           !Return the resulting string** **See Also:** [[procedure prototypes.htm|PROCEDURE Prototypes]] [[data declarations and memory allocation.htm|Data Declarations and Memory Allocation]] [[procedure overloading.htm|Procedure Overloading]] [[class object declaration .htm|CLASS]] [[routine declare local subroutine .htm|ROUTINE]] [[map declare procedure prototypes .htm|MAP]]