## Wednesday, November 16, 2011

### HP 15C Programming Tutorial - Part 5: Subroutines

Subroutines

A subroutine is a set a of repeated instructions that is called within a main program. The subroutine is keyed in only once which saves memory. There is a return command at the end of each subroutine which allows the calculator to return to the point where the subroutine is called.

For example, let's program the path as someone enters a house after getting home from work. Typically, the person has to open doors many times. Let this be the subroutine. A program diagram would look like this:

`Person Enters House     ↓ Subroutine: Open Door     ↓Enter Living Room     ↓Go to the Bedroom     ↓Subroutine: Open Door     ↓ Enter Bedroom     ↓ Go to Closet      ↓ Subroutine: Open Door     ↓Put down briefcase, purse, and/or backpack     ↓ Relax - ENDSubroutine:  Open DoorIs the door locked?Yes: Use appropriate key to unlock itNo: Go onto the next step     ↓Use hand to turn the knob      ↓Push the door     ↓Walk in to the next room     ↓RETURN`

Subroutines on the HP 15C

In Programming Mode, press the [GSB] key (3rd row, 2nd key) followed by the label (0-9, .0-.9, A-E). According to the HP 15C manual, GSB stands for go to subroutine.

Subroutines can call other subroutines, subroutines of subroutines are called nested subroutines. On the HP 15C, subroutines can be nested seven levels deep.

Part 5 contains two programs to illustrate the use of subroutines. In the second program presented, we will make use of a subroutine help to program the Quadratic Formula.

Using a Subroutine

Let f(x) = cos^-1 (sin 2x/π) * sin^-1 (cos (2x/π)^2) * (2x/π)^3 ; where x is in radians. Calculate f(1), f(2), and f(3).

Note that the expression 2x/π repeats. Let's use this as a subroutine.

In this example, we will label the main program C and use Label 2 as the subroutine.

Program Listing:

`Key Code				Key 001	42	21	13		LBL C002		43	8		RAD003		44	0		STO 0004		32	2		GSB 2005			23		SIN006		43	24		COS^-1007		45	0		RCL 0008		32	2		GSB 2009		43	11		x^2010			24		COS011		43	23		SIN^-1012			20		× 013		45	0		RCL 0014		32	2		GSB 2015			3		3016			14		y^x017			20		× 018		45	0		RCL 0019		32	2		GSB 2020			10		÷021		43	32		RTN022	42	21	2		LBL 2	*subroutine starts023			2		2024			20		× 025		43	26		π026			10		÷027		43	32		RTN`

Instructions

Enter x. Press [ f ] [10^x] (C).

Examples

f(1) ≈ 0.4413
f(2) ≈ -0.0243
f(3) ≈ -1.3169

This program find the roots the quadratic equation:

ax^2 + bx + c = 0

Let's assign memory registers R1, R2, and R3 as follows:

R1 = a
R2 = b
R3 = c

Values for R1, R2, and R3 are stored before program execution.

The other memory registers used in this program are R0, R4, and R5, where:

R0 = discriminant = b^2 - 4ac

If R0 < 0, then
R4 = the real part of the complex roots x ± yi
R5 = the imaginary part of the complex roots x ± yi

If R0 ≥ 0 then
R4 = one of the real roots
R5 = one of the real roots

Program Instructions

`Key Code			Key001	42	21	11	LBL A002		45	2	RCL 2003		43	11	x^2004			4	4005	45	20	1	RCL × 1006	45	20	3	RCL × 3007			30	-008		44	0	STO 0009			31	R/S010	43	30	2	x<0?	* TEST 2011		22	0	GTO 0	* R0 < 0 - complex roots012			4	4	* real roots013			10	÷014		45	1	RCL 1015		43	11	x^2016			10	÷017			11	√018		32	1	GSB 1019			34	x<>y020			40	+021		44	4	STO 4022			31	R/S023		43	36	LSTx024			2	2025			20	× 026			30	-027		44	5	STO 5028		43	32	RTN029	42	21	0	LBL 0	* complex roots030		32	1	GSB 1031		44	4	STO 4032			31	R/S033		45	0	RCL 0034		43	16	ABS035			11	√036			2	2037			10	÷038	45	10	1	RCL÷ 1039		44	5	STO 5040		43	32	RTN041	42	21	1	LBL 1	* subroutine starts here042		45	2	RCL 2043			16	CHS044			2	2045			10	÷046	45	10	1	RCL÷ 1047		43	32	RTN`

ax^2 + bx + c = 0

Instructions:
1. Store a in R0, b in R1, and c in R2.
2. Press [ f ] [ √ ] (A).
3. The discriminant is displayed (R0).
4. Press [R/S] displays R4.
5. Press [R/S] displays R5.

If R0 ≥ 0, R4 and R5 are the real roots of the quadratic equation.

If R0<0, R4 is the real part of the complex root, and R5 is ±imaginary part of the complex root.

Examples:

1. x^2 + 4x + 6
R1 = 1, R2 = 4, R3 = 6
Result: R0 = -8, R4 = -2, R5 ≈ 1.4142
The roots are -2 ± √2 i

2. x^2 - 5x + 3
R1 = 1, R2 = -5, R3 = 3
Result: R0 = 13, R5 ≈ 0.6972, R4 ≈ 4.3028
The roots are approximately 0.6972 and 4.3028.

I hope found Part 5 helpful, and I'll see you for Part 6! Eddie

This tutorial is property of Edward Shore. © 2011

### HP 71B: Basic Expanded RPN Program (RPNT)

HP 71B: Basic Expanded RPN Program ( RPNT ) Introduction This program is an expansion of RPNBASIC that was posted on May 12, 2024....