13.07.2015 Views

Turbo Basic

Turbo Basic

Turbo Basic

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

DispHelpText:oldcsrx = POS(O) : oldcsry = CSRLINLOCATE 22,1PRINT "This program was shipped with a fantastic manual"PRINT "carefully prepared by professional writers at great expense."PRINT "It contai ns more i nformati on than you will ever need."INPUT "Use it. Press Enter to proceed.", dummy$LOCATE 22,1 : PRINT SPACE$(320)LOCATE oldcsrx, oldcsryRETURNNow that the trap subroutine has been declared (the code that will get controlwhen F I is pressed), tum on trapping with the KEY(n) statement:KEY (10) ONAfter executing this statement, <strong>Turbo</strong> <strong>Basic</strong> goes into event-checldng mode.Between executing every subsequent statement, it does a quick check to see if F Ihas been pressed. If not, execution continues normally with the next statement. Ifit finds F I pressed, subroutine DispHelpText is called. When it RETURNs, controlpasses to the next statement in the interrupted sequence.When an event trap occurs, an implicit "trap stop" statement is executed to keepthe trap subroutine from being called repetitively from within itself (and filling upthe stack). The closing RETURN of the handling subroutine automatically performsan implicit "trap on" statement unless the subroutine, in its body, executes anexplicit "trap off." You can substitute the words PEN, PLAY, KEY, STRIG, TIMER,and COM for the word "trap."Fine-Tuning Event TrappingEvent trapping is a powerful tool, but at a price. Since your program must checkbetween the execution of every statement for the indicated event(s), processingslows down, which, depending on the application, may produce an unacceptablespeed penalty. In addition, a program that does event trapping causes the compilerto generate more code (exactly 1 byte more per statement) than it would for aprogram with no event-trapping statements.You can control <strong>Turbo</strong> <strong>Basic</strong>'s generation of event-checking code with the$EVENT metastatement. Use $EVENT in programs that need to do event trapping,but not everywhere. $EVENT OFF prevents event-trapping code from beinggenerated until the appearance of an $EVENT ON metastatement.To illustrate, consider a sort routine in the accounting program described earlier.Sorts are slow enough already without being interrupted between every statementto see if F I has been pressed. Furthermore, since the sort doesn't require any userinput, what's the point in putting a help message on the screen?Event Trapping 391

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!