5.4 Prematurely Returning from a Procedure


5.4 Prematurely Returning from a Procedure

The HLA exit and exitif statements let you return from a procedure without having to fall into the corresponding end statement in the procedure. These statements behave a whole lot like the break and breakif statements for loops, except they transfer control to the bottom of the procedure rather than out of the current loop. These statements are quite useful in many cases.

The syntax for these two statements is the following:

 exit procedurename; exitif( boolean_expression ) procedurename; 

The procedurename operand is the name of the procedure you wish to exit. If you specify the name of your main program, the exit and exitif statements will terminate program execution (even if you're currently inside a procedure rather than the body of the main program).

The exit statement immediately transfers control out of the specified procedure or program. The conditional exitif statement first tests the boolean expression and exits if the result is true. It is semantically equivalent to the following:

      if( boolean_expression ) then           exit procedurename;      endif; 

Although the exit and exitif statements are invaluable in many cases, you should try to avoid using them without careful consideration. If a simple if statement will let you skip the rest of the code in your procedure, by all means use the if statement. Procedures that contain a lot of exit and exitif statements will be harder to read, understand, and maintain than procedures without these statements (after all, the exit and exitif statements are really nothing more than GOTO statements, and you've probably heard already about the problems with GOTOs). exit and exitif are convenient when you got to return from a procedure inside a sequence of nested control structures and slapping an if..endif around the remaining code in the procedure is not possible.




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net