TI Basic Modular Coding Strategies

As a programmer in a math class that requires a graphing calculator, I end up writing a lot of useful programs. But limited to a variant of BASIC, I quickly run into problems when trying to modularize and refactor my code. Here are some techniques I have developed to work around BASIC's basic limitations (ha).

The Sub-Program Method

One of the few capabilities that TI Basic has is the ability to run other programs inside itself. This can be coupled with the fact that variables always retain state to great effect. For example, if I need to simplify a radical inside a program, I could store the number in . Then I would call a program which would perform the necessary calculations and then store the coefficient in and the number under the square root in . Control would then jump back to the main program.

In order to use this to its full advantage, it is helpful to pick unlikely-to-be-used-elsewhere variable names so that you do not accidentally overwrite an important number. Although it initially seems like you are confined to only 26 variables, there are a few others you can take advantage of. The greek letter Theta, , is on the keyboard for some reason, so I often store numbers in that when jumping between programs. Besides using lists, numbers can also be stored in matrices, giving you many more variables to work with.

The "Goto" Method

Such pun. Although lacking functions, BASIC does provide support for Labels, which allow you to jump between sections of the program. While initially this seems next to useless, by making use of flag variables, function functionality can be simulated. Here is a simple example.

goto B

Lbl F
if Str1="A"
  goto Y
  goto Z

Lbl A
disp "IN LABEL A"
goto F
Lbl Y
goto C

Lbl B
disp "IN LABEL B"
goto F
Lbl Z
goto A

Lbl C

The function, F, is declared at top, to skip over it there is an initial goto to the start of the program B. B calls the function then goes to A, which also causes the function before jumping to the bottom of the program. The output of this program is:


The basic (ha) mechanic at work here is that the last label the control flow is in before the function is called is stored in a string. Then, at the end of the function it checks the state of the string and jumps back to the appropriate label. It is important to note that control cannot jump back to the same label that calls it, since an infinite loop would occur. Instead, labels must be added after the function call in the callee label, immediately after the call to F. So in Lbl A, F is called, then control jumps down to Y, which says goto C.

TI BASIC has many limitations, but since it's all we have, we have to do the best we can with it.