Actions

Actions

Actions

Actions

# HP 20s Calculator - Program Subroutines

Calculator symbol key
The procedures in this document use the following text to represent symbol keys:
 Key Description Text representation Right shift key RS Left-shift key LS Divide divide
Introduction
A program is composed of one or more routines. A routine is a functional unit that accomplishes a specific task. As programs get more complicated, it helps to break them into smaller pieces. This makes a program easier to write, read, understand, and alter.
A routine typically starts with a label (LBL) and ends with an instruction that alters or stops program execution, such as RTN or GTO.
A subroutine is a routine that is called from (executed by) another routine and returns control to that same routine when it finishes. The subroutine must start with a LBL and end with a RTN. A subroutine can call other subroutines.
If a subroutine is at the end of program memory and does not end with RS, then RTN, control is still transferred to the step after the originating XEQ when routine completes. It is as if the subroutine had ended with RS, then RTN.
Calling subroutines
Use XEQ label to call a specific subroutine. The subroutine must start with the label A through F or 0 through 9. Searching begins at the XEQ and proceeds down the program, wrapping around through line 00 until the label is found. Within a program, XEQ label transfers execution of a running program to the program line containing that label, wherever it may be. The program continues running from the new location. Then, at the next RS, RTN statement, execution returns to the line after the originating XEQ continues.
For example, to write a program that calculates the average slope between x2 and x2 on the graph (in Figure 2), where y = x2 - sin x, use the following formula:
Figure : Formula for calculating slope
Figure : Graph of slope
The solution requires two calculations of the expression x2 - sin x (for x = x1 or x = x2). Since the solution includes an expression that must be repeated for both values of x, create a subroutine to execute the repeated keystrokes and save space in program memory. The program assumes that x2, INPUT, x2 has been entered before executing the program and that the calculator is in Radians mode (RS, then RAD).
 Keys Display Description Press LS, then PRGM Enters Program mode Press RS, then CLPRGM 00- Clears program memory Press RS, LBL, then C 01- 61 41 C Names program "C" Press STO, then 2 02- 21 2 Stores the displayed value (x2) in R2 Press LS, then SWAP 03- 51 31 Swaps (x2 for x1) Press STO, then 1 04- 21 1 Stores displayed value (x1) in R1 Press C 05- 71 Clears display so there is no hidden value or : annunciator when program is complete Press RCL, then 2 06- 22 2 Recalls x2 Press XEQ, then 5 07- 41 5 Executes subroutine to calculate x22 - sin x2 Press [-] 08- 65 (x2-SINX2) - Press RCL, then 1 09- 22 1 Recalls x1 Press XEQ, then 5 10- 41 5 Executes subroutine again to calculate x12 - sin x1 Press = 11- 74 (x12 - sin x2) - (x12> - sin x1) Press divide 12- 45 Divides result by ... Press ( 13- 33 Reorders precedence Press RCL, then 2 14- 22 2 Recalls x2 Press [-] 15- 65 x2 - ... Press RCL, then 1 16- 22 1 Recalls x1. Closing parenthesis not required because = follows. Press = 17- 74 ((x22 - sin x2) - (x22 - sin x1)) / (x2 - x<1) Press R/S 18- 26 Stops Press RS, LBL, then 5 19- 61 41 5 Label 5 starts the subroutine Press STO, then 0 20- 21 0 Stores the displayed value in R0 Press ( 21- 33 Reorders priority Press LS, then x2 22- 51 11 Squares the displayed value Press [-] 23- 65 Subtracts Press RCL, then 0 24- 22 0 Recalls contents of R0 Press SIN 25- 23 Calculates the sine Press ) 26- 34 Closing parenthesis required to evaluate x2 - sin x Press RS, then RTN 27- 61 26 Ends subroutine and returns to line following originating XEQ Press LS, then SHOW 7EE9 Checksum (see Checksum below) Press LS, then PRGM Exits Program mode
To execute the program using 3 and 4 as x1 and x2, press 3, INPUT, 4, XEQ, then C. The result is 7.8979. To exit Radians mode, press RS, then DEG
Nested subroutines
A subroutine can call another subroutine, and that subroutine can call yet another subroutine. This "nesting" of subroutines - the calling of a subroutine from within another subroutine - is limited to four levels of subroutines. The operation of nested subroutines is shown in Figure 1:
Figure 1: Nested subroutines
An error will occur if an attempt is made to execute a subroutine nested more than four levels deep. The message "Error - Sub" appears in the display.
Checksum
After a program is entered, check to see if the keystrokes are entered correctly by comparing the checksum listed in this document to the checksum created by the program. The checksum is a unique hexadecimal value assigned to the specific keystrokes entered. To view the checksum, press and hold LS and SHOW for a moment while in Program mode.

Actions

## hp-online-communities-portlet

Actions

### Support Forum

Join the conversation! Find Solutions, ask questions, and share advice with other HP product owners. Visit now

Actions