| **Navigation:**  [[introduction.htm|Language Reference]] > 2 - Program Source Code Format > Program Format >====== ROUTINE (declare local subroutine) ====== | [[data begin routine local data section .htm|{{btn_prev_n.gif|Previous page}}]][[introduction.htm|{{btn_home_n.gif|Return to chapter overview}}]][[end terminate a structure .htm|{{btn_next_n.gif|Next page}}]] | | || | //label// | **ROUTINE** | | | **[ DATA** | | | //local data// | | | **CODE ]** | | | //statements// | {{blk2blue.jpg|blk2blue.jpg}} | **ROUTINE** | Declares the beginning of a local subroutine. | | //label// | The name of the ROUTINE. This may not duplicate the label of any PROCEDURE. | | **DATA** | Begin data declaration statements. | | //local data// | Declare Local data visible only in this routine. | | **CODE** | Begin executable statements. | | //statements// | Executable program instructions. | | | | **Remarks:** **ROUTINE** declares the beginning of a local subroutine. It is local to the PROCEDURE in which it is written and must be at the end of the CODE section of the PROCEDURE to which it belongs. All variables visible to the PROCEDURE are available in the ROUTINE. This includes all Procedure Local, Module Local, and Global data. **Clarion#** **I**n Clarion#, the DATA and CODE keywords are required. A ROUTINE may contain its own local data which is limited in scope to the ROUTINE in which it is declared. If local data declarations are included in the ROUTINE, they must be preceded by a DATA statement and followed by a CODE statement. Since the ROUTINE has its own name scope, the labels of these variables may duplicate variable names used in other ROUTINEs or even the procedure containing the ROUTINE. A ROUTINE is called by the DO statement followed by the label of the ROUTINE. Program control following execution of a ROUTINE is returned to the statement following the calling DO statement. A ROUTINE is terminated by the end of the source module, or by another ROUTINE or PROCEDURE. The EXIT statement can also be used to terminate execution of a ROUTINE's code (similar to RETURN in a PROCEDURE). A ROUTINE has some efficiency issues that are not obvious: ·DO and EXIT statements are very efficient. ·Accessing procedure-level local data is less efficient than accessing module-level or global data. ·Implicit variables used only within the ROUTINE are less efficient than using local variables. ·Each RETURN statement within a ROUTINE incurs a 40-byte overhead. **Example:** **SomeProc PROCEDURE** ** CODE** ** !Code statements** ** DO Tally                !Call the routine** ** !More code statements** **Tally ****ROUTINE****            !Begin routine, end procedure** ** DATA** **CountVar BYTE            !Declare local variable ** ** CODE** ** CountVar += 1           ! increment counter** ** DO CountItAgain         !Call another routine** ** EXIT                    !and exit the routine** **See Also:** [[procedure define a procedure .htm|PROCEDURE]] [[exit leave a routine .htm|EXIT]] [[do call a routine .htm|DO]] [[data begin routine local data section .htm|DATA]] [[code begin executable statements .htm|CODE]]