104 Section 9: Subroutines

Example: Nesting. The following subroutine, labeled ―.4‖, calculates the value of the expression x2 + y 2 + z 2 + t 2 as part of a larger calculation in a

larger program. The subroutine calls upon another subroutine (a nested subroutine), labeled ―.5‖, to do the repetitive squaring.

The program is executed after placing the variables t, z, y, and x into the T-, Z-, Y-, and X-registers.

Keystrokes

´b.4

x G.5

G.5

G.5

¤

n

´b.5

®

x

+

n

Start of main subroutine.

x2.

Calculates y2 and

x2 + y2.

Calculates z2 and

X2 + y2 + z2.

Calculates t2 and

x2 + y2 + z2 + t2.

x2 + y 2 + z 2 + t 2

End of main subroutine; returns to main program.

Start of nested subroutine.

Calculates a square and

adds it to current sum of squares.

End of nested sub-routine; returns to main subroutine.

If you run the subroutine (with its nested subroutine) alone using x = 4.3, y = 7.9, z = 1.3, and t = 8.0, the answer you get upon pressing G.4 is 12.1074.