102 Section 9: Subroutines

Subroutine Limits

A subroutine can call up another subroutine, and that subroutine can call up yet another subroutine. This ―subroutine nesting‖—the execution of a subroutine within a subroutine—is limited to stack of subroutines seven levels deep (this does not count the main program level). The operation of nested subroutines is as shown below:

Main Program

bA b1 b2 b3 b4

G1G3

G2G4

n n n n n End

Examples

Example: Write a program to calculate the slope of the secant line joining points (x1, y1) and (x2, y2) on the graph shown, where y = x2 - sin x (given x in radians).

The secant slope is:

y2 y1 , or (x2 2 − sin x2 ) − (x12 − sin x1 )

x2 x1x2 x1

The solution requires that the equation for y be evaluated twice—once for y1 and once for y2, given the data input for x1 and x2. Since the same calculation must be made for different values, it will save program space to call a subroutine to calculate y.

The following program assumes that x1 has been entered into the Y-register and x2 into the X-register.