Subprograms in the calculator are similar in some respects to GoSub routines in Visual Basic for Applications: there are no explicit parameters or arguments, and variables are shared with the main program. However, subprograms are stored separately from their main programs and have their own set of Lbl/Goto labels.
Subprograms in the calculator are invoked with the statement:
Prog "routinename"
where routinename is the name of the program to be invoked. When the named program exits through the Return statement, or through reaching its end, execution resumes at the statement after the Prog statement. Programs can be nested up to ten deep; however, running a program from the calculator menu counts towards this limit of ten. (This does not mean that only ten Prog statements can be executed: it means ten Prog statements can be active simultaneously.)
Typically, calculator program accept parameters in specific variables, and return results in specific variables, and destroy further specific variables for their internal variables. The pseudo-variable Ans is also available for return values. Evaluating an expression, but not storing the value into a variable, sets the Ans pseudo-variable. The variables used by a program constitute the calling sequence for the program. A typical program description will state that certain inputs must be present in certain variables before the program is called, certain outputs will be present in certain variables when the program returns, and the contents of certain variables will be destroyed. For example, the NEXTFACT program in the Factorization program starts with the following comments:
; Variables: ; A is on entry the number for which a factor is to be found, and is ; on exit the quotient of the original A and the factor found ; B is on entry the lowest number that is a possible factor (0 and 1 ; are assumed to be 2), and is on exit the smallest factor found
These comments describe the calling sequence for this program.
It is possible to simulate local storage for subprograms in the calculator. This allows programs change their internal variable use without impacting their clients. The typical arrangement is for all the programs to agree to store the original copies of work variables in a specified List, indexed by a specified variable.
Lists will be covered in detail in the section Data Structures I. A quick explanation will be sufficient for explaining the simulation of local variables in the calculator. A list (also called a vector or a one-dimensional array) is a set of variables identified by a numeric index value, rather than by name. The index value must lie between the minimum and maximum possible index values. In the case of the calculator’s Lists, the minimum index value is 1 and the maximum index value is the size of the List. Calculator Lists have a maximum of 255 elements. A particular List element is referred to as List n[k], where n is the list number (1 to 6, inclusive), and k is the index value.
Each subprogram then stores the original contents of variables it will use in the List, and updates the index variable. Suppose, for example, all the programs are written to use List 6 for storage and W as the list index. The main program would include the sequence:
Seq(0,A,1,255,1)→List 6 0→W
This initializes List 6 to have 255 elements, indexed [1] through [255], and initializes W, the “current top of list” index, to zero. Thereafter, a subprogram that for example uses I, J, X, Y, and Z as temporary variables, would include a sequence like:
I→List 6[W+1] J→List 6[W+2] X→List 6[W+3] Y→List 6[W+4] Z→List 6[W+5] W+5→W
when it starts, and a sequence like:
W-5→W List 6[W+5]→Z List 6[W+4]→Y List 6[W+3]→X List 6[W+2]→J List 6[W+1]→I
when it exits. The effect of these sequences is to save the original contents of work variables, then restore them. Note that the “current top of list” index is also updated, so further programs using this technique will not overwrite the saved values. Overall, the effect of this technique is to maintain the contents of the work variables across calls to subprograms — effectively giving each subprogram its own set of variables. A similar technique can be used for passing parameter values, as well, avoiding the need to reserve specific variables for parameters.
This technique of saving and restoring the original contents of work variables is used in the Physics programs in this site. These programs use a set of subroutines to save and restore blocks of five variables; the subroutines use List 6 as the variable storage area, and the variable Z as the list index.
[ Previous page | Top of page | Next page ]
Copyright © 2001 Brian Hetrick
Page last updated 3 March 2002.
Tutorial
Building Blocks I
Control Flow II
Subprograms
Calculator
Basic I/O
Algorithms
A First Program
Answers
Modularization
Data Structures I
Recursion
Program Attributes
Building Blocks II
Algorithm Analysis
Structuring
Data Structures II
Abstract Types
Objects
Problem Analysis
Reference Card
![]()