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.

program, n doesn't hold the 1 the main program put there earlier, but holds thevalue of Employee Count + 1 as a result of the FOR/NEXT loop in CalcDeductions.In keeping with its informal style, BASIC doesn't'require that you "declare"variables (that is, specify in a statement someplace that you are about to use avariable named such-and-such). Even if you religiously track your program's variables,a single slip of a finger can foul things up.To help prevent this problem, <strong>Turbo</strong> <strong>Basic</strong> allows 1ocal" variables within proceduresand functions. A local variable, unlike a global variable, only exists in theroutine in which it is declared. Local variables alone are a sufficiently good reasonto swear off subroutines forever. For example, study function AddReceipts:DEF FNAddReceiptsLOCAL x, y, totalFOR x = 1 TO 12FOR y = 1 TO 30total = total + receipts(x,y)NEXT yNEXT xFNAddReceipts = totalEND DEFSince variables x and yare declared as local to FNAddReceipts, you can usevariable names x and y elsewhere in this program (programmers with a mathematicalbent are wont to use x and y for every variable) without affecting the values of xand y in FNAddReceipts (or vice versa).FNAddReceipts' local variables exist only while that function is being calledbeforeand after that function, it's as though they never existed. To illustrate, considerthis code segment that calls FNAddReceipts:x = 35thisYear = FNAddReceiptsPRINT xThe fact that the name x has been given to other variables in the process ofcalculating a return value for FNAddReceipts doesn't affect the x in lines one andthree. Through the magic of stack allocation, the x in FNAddReceipts is a separatevariable, one that doesn't exist after the function returns.Local variables must be declared before any executable statements in a procedureor function. Local variables can be arrays; simply dimension the array afterdeclaring it as local.SUB DUI11T1YLOCAL a, locArray()DIM DYNAMIC locArray(50)ERASE locArrayEND SUB92 <strong>Turbo</strong> <strong>Basic</strong> Owner's Handbook

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

Saved successfully!

Ooh no, something went wrong!