Subprograms

A long program can get complicated to manage when it's all in one piece. For this reason, it's often easier to write code with a main program that does its job by running several other programs called subprograms. In fact, it's fairly easy to start using this method from the planning stage.

There are other benefits to using subprograms, of course. It's good to define a subprogram for a task that needs to be done more than once. Also, a subprogram can run itself recursively, if necessary.

Of course, you don't want to release a finished program in lots of pieces, so at the end of the day, you want to incorporate the subprograms in the main program. If the subprogram isn't recursive and is only used once, this is easy: just use 2nd RCL to recall the subprograms into the place where they're used, and remove their Prgm..EndPrgm parts. See the next section for what to do in more difficult cases.

Defining a Subprogram

You can also define a subprogram right in your main program. This is done using the Define statement:

:Define subprgm()=Prgm
: © subprogram code
:EndPrgm

It's easiest if all the subprograms are defined at the same time, near the beginning of the program, but it doesn't really matter where you do it as long as you define the subprogram before you use it.

Cleaning Up

It's important to realize that a subprogram defined in this way is just like a regular variable. If you don't do anything, then after the main program finishes running, the subprogram will be left hanging around, which is usually not a good thing. As with other variables, there are several ways to clean this up:

  • Use Local to declare the subprograms as local variables (e.g. Local subprgm1, subprgm2).
  • Use DelVar at the end of the program to delete the subprograms (e.g. DelVar subprgm1, subprgm2).
  • Give the programs one-letter names, and use NewProb at the end of the program.

See Setup and Cleanup for more information on how to do this.

<< System Variables Overview Saving Data >>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.