Swiss Micros DM42, Free42, Plus42: FUNC command
Turning Programs into User Functions
The FUNC command saves the stack contents and treats the program like it is a function. This allows the program to work with a temporary stack which is "deleted" when the program reaches either END or RTN (and RTNYES, RTNN), while storing the original contents of the X stack in the L (LAST X) stack.
FUNC requires a two digit argument. The first digit is the stack levels used in input, while the second digit is the stack levels used in output.
FUNC 11: Takes the argument from the X stack and returns the answer in X stack, while leaving the rest of the stack intact.
X: f(x), Y: y0, Z: z0, T: t0, Last X: x0
FUNC 21: Takes the arguments from the X and Y stacks and returns the answer in the X stack.
X: f(x,y), Y: z0, Z: t0, T: t0, Last X: x0
FUNC22: Takes the arguments from the X and Y stacks and returns the answers in the X and Y stacks.
X: x from f(x,y), Y: y from f(x,y), Z: z0, T: t0, Last: x0
Example 1: f(x,y) = (y + x)/(y - x)
Stack set up:
T = 24
Z = 15
Y = 45
X = 21
PR1: Program
00 { 17-Byte Prgm }
01▸LBL "PR1"
02 -
03 ENTER
04 ENTER
05 LASTX
06 +
07 LASTX
08 +
09 X<>Y
10 ÷
11 RTN
12 END
BEFORE PR1:
T= 70.0000
Z= 15.0000
Y= 45.0000
X= 21.0000
AFTER PR1:
T= 15.0000
Z= 15.0000
Y= 15.0000
X= 2.7500
FN1: Using FUNC21
00 { 20-Byte Prgm }
01▸LBL "FN1"
02 FUNC 21
03 -
04 ENTER
05 ENTER
06 LASTX
07 +
08 LASTX
09 +
10 X<>Y
11 ÷
12 RTN
13 END
BEFORE FN1:
T= 70.0000
Z= 15.0000
Y= 45.0000
X= 21.0000
AFTER FN1:
T= 70.0000
Z= 70.0000
Y= 15.0000
X= 2.7500
Example 2: f(x,y) = x * y * √(x^2 + y^2)
Stack set up:
T = 64
Z = 11
Y = 15
X = 25
Note: The →POL command calculates that x^2 + y^2 on the x stack
PR2: Program
00 { 17-Byte Prgm }
01▸LBL "PR2"
02 STO ST Z
03 X<>Y
04 STO× ST Z
05 →POL
06 RCL× ST Z
07 RTN
08 .END.
BEFORE PR2:
T= 64.0000
Z= 11.0000
Y= 15.0000
X= 25.0000
AFTER FN2:
T= 64.0000
Z= 375.0000
Y= 1.0304
X= 10,933.0348
FN2: Using FUNC21
00 { 20-Byte Prgm }
01▸LBL "FN2"
02 FUNC 21
03 STO ST Z
04 X<>Y
05 STO× ST Z
06 →POL
07 RCL× ST Z
08 RTN
09 END
BEFORE FN2:
T= 64.0000
Z= 11.0000
Y= 15.0000
X= 25.0000
AFTER FN2:
T= 64.0000
Z= 64.0000
Y= 11.0000
X= 10,933.0348
As we can see, the FUNC command is a powerful command that allows programs to act like user functions, and is a great function for the DM42, Free42, and Plus42. Unfortunately, FUNC is not available on the original HP 42S.
Until next time, happy programming,
Eddie
All original content copyright, © 2011-2022. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.