HP Prime Programming Tutorial #1: LOCAL, RETURNOver the next month, maybe month and a half, I plan to post programming tutorials for the HP (Hewlett Packard) Prime. If you have programmed with the HP 38G, 39g, or 39gii, this language will be similar to those. The programming language for the Prime is named the HP Prime Programming Language (HPPP). Throughout this tutorial, I am going to use the latest version of the software. How to start writing a program: 1. Press Shift + 1 (Program). 2. Press New. It is the second touch key. 3. Enter the name of the program. Pressing the ALPHA key twice will turn on UPPERCASE ΑLPHA-LOCK. Pressing ALPHA, Shift, ALPHA will turn on lowercase alpha-lock. To exit any lock, press the ALPHA key one more time. When happy with the name, press Enter. In short we are defining a custom function: SQIN(x) = 1/x^2 Commands: RETURN: returns a result to the stack (home page).BEGIN . Each line containing a command generally must end with a semicolon (. The layout is generally like this: EXPORT program_name(arguments) BEGIN commands and comments go here END. SQIN Our first program is SQIN. Anything in the line following the two slashes is ignored in running the program. Letters. The program name must start with a letter. numbers.Rules for Program Names: 1. SQIN takes a number. .). because "Hello World" programs are so 2000s. then calculates the reciprocal. and the underscore character (_) only. Comments can be typed.END structure. 2. Structure of a HP Prime Program A HPPP program is encased of an EXPORT . vectors. The are designated by two forward slashes. lists. The slashes are typed by pressing the Divide key ( ÷ ). squares it. A semicolon can by type by pressing ALPHA then the Plus key ( + ). You can return numbers. 04 SQIN(36) returns . Access: Tmplt. END.Textbook Entry. the variables are created. 2. How to run the programs: Home Mode . 2nd key from the left. Type the name. Or use the Toolbox (top row of white keys. Home Mode .RPN Entry: Enter each argument. it looks like a tool box). 1. Access: Tmplt. used. possibly displayed during program execution. 1. LOCAL . select the User touch key. separate each entry by pressing the Enter key. Variable. and deleted at program termination. 4. Block.matrices. or a combination of these times. the RPN stack would like this: 4: argument_1 3: argument_2 2: argument_3 1: argument_4 TEST(4) to run the program. and input the required arguments. strings. if the program TEST has four arguments. I use the Check command all the time. CAS Mode: Type the program name. In other words. Follow the name with parenthesis and enclose the required arguments. Tip: You can check the syntax of the program just by pressing the Check soft key in the program editor. For example. MOPMT LOCAL: Declares any variables to be local to the program. and in the parenthesis state the number of arguments. HP Prime will inform you if there is a syntax error and attempt to point you to the error. Examples to try with SQIN: SQIN(5) returns . the Prime states "No errors in the program". RETURN All the program code in this tutorial series will be shown in Courier font.Algebraic Entry.000771604938 The next program will demonstrate the concept of local variables. If there are no syntax errors. Home Mode . EXPORT SQIN(X) BEGIN RETURN 1/X^2. select the program. the interest rate (R). 9.5. The arguments are: the loan amount (L). TEXTOUT_P. For example: LOCAL K:=1.M) BEGIN LOCAL K:=R/1200. 360) returns 1661. Tip: Use RETURN.317437565 MOPMT(370000. stores 1 in K and makes K a local variable. 30) returns 150. Examples: MOPMT(4000. 3. K:=L*K/(1(1+K)^M). EXPORT MOPMT(L.46534383 Try this and next time in the series I will highlight other things we can do with HPPP.5. and the number of months (M). and PRINT to return custom strings. RETURN "Payment ="+K.R. Thanks! Eddie This blog is property of Edward Shore. messages. and calculations. 2013 .Tip: You can declare local variables and assign an initial value at the same time. END. which combine results. Parts are connected with a plus sign. MOPMT calculates the monthly payment of a loan. "+L0(3)). PRINT. and the FOR loop. MSGBOX MSGBOX: MSGOX takes a string a makes a pop-up message box. The HP Prime's default list variables are designated L0 through L9. ** Thanks to Thomas Lake for pointing out my typo. Picks may be repeated.HP Prime Programming Tutorial #2: MSGBOX. we will cover MSGBOX. old-school combination locks.Eddie (3/21/2014) Other commands that are featured: RANDINT(n. This program gives three digit combinations through the use of MSGBOX. Access: Cmds.** MSGBOX("SECRET: "+L0(1)+". b) generates a list of n integers between a and b. EXPORT COMLOCK() BEGIN LOCAL L0. a. L0:=RANDINT(3."+L0(2)+".39). 6. Program execution stops until you press a key to acknowledge the message.0. 8. PRINT. MSGBOX The program COMLOCK: Imagine that you are in charge of setting the combinations for the good. I/O. In this session. You can leave n out if you desire a single random integer. IF-THEN-ELSE. END. IF-THENELSE. FOR Welcome to another programming tutorial for the HP Prime. Here is a sample output for COMLOCK: . Apologies for any inconvenience . END. IF THEN ELSE Tip: You can leave out the ELSE part if you only want to test to see if a condition is true. Press a button to exit. IF THEN. Access: Tmplt. or a combination of both onto the Prime's Terminal screen. Branch. result. 2. the program will end on the terminal (text output) screen. 2. Note that the double equals is needed to check equality. ≤. ELSE do if the condition is false. by pressing Shift. Access <. ==. PRINT PRINT: The PRINT command prints a sting. 1. 6. If PRINT is used. Branch. .IF-THEN-ELSE IF-THEN-ELSE: Program structure: IF condition THEN do if the condition is true. 2. Access the simple IF-THEN structure by pressing Tmplt. etc. -2. sorry for not being original guys and gals).5-1.").-8) returns: Roots are real. LOOP.-4. EXPORT QROOTS(A. PRINT Tip: To clear the terminal screen. demonstrates the use of IF-THEN-ELSE and PRINT. 3. and then pressing the Divide ( ÷ ) button.5.2360679775 FOR This section will explore the basic FOR structure: FOR variable FROM start TO end DO commands. PRINT((B?D)/(2*A)). the variable increases by one. Here I set the setting variable HComplex to 1. The loop terminates when variable=end. END.32287565553*i -2. Access: Cmds. Examples: QROOTS(1. holding it.C) BEGIN LOCAL D. 3. PRINT(). IF D?0 THEN PRINT("Roots are real. The program QROOTS (yet one more quadratic solver. 9.32287565553*i QROOTS(2. This is a good way to clear the terminal screen and I usually use this at the beginning of any program if PRINT is going to be used later on. type PRINT(). Each time a loop finishes. PRINT((B+?D)/(2*A)). D:=B^24*A*C. END.8) returns: Roots are complex. I/O. 1. 6. END.2360679775 -1. which allows for complex number results."). HComplex:=1.5+1. FOR .B. Access: Tmplt. All the commands in the loop will be executed a set number of times.You can access the terminal screen at any time by pressing the ON button. ELSE PRINT("Roots are complex. Access: Toolbox. This concludes this part of the HP Prime Programming Tutorial. . Strings.The program SUMDIV takes any integer and adds up the sum of its divisors. mdiv:=CAS. 3. 1. which I had in this program was unnecessary. 1. DIM The program: EXPORT SUMDIV(N) BEGIN LOCAL S:=0. DIM: returns the dimensions of a sequence.ldiv. FOR K FROM 1 TO ldiv(1) DO S:=S+mdiv(K). Featured Commands in SUMDIV: idivis: idivis(integer) returns a sequence of all of the divisors if integer. ldiv:=DIM(mdiv). 2. and 6. string. The sum is 28. Divisors Any CAS command used in programming will be preceded by "CAS. DIM returns the list {number of rows. END. . DIM returns the length in a list {length}.K. Access: Cmds. DIM must be used instead of SIZE to prevent a Bad Argument error. number of columns}. For matrices. RETURN S. END. Integer. 9. 5. SUMDIV(85) returns 108. the divisors of 12 are 1.Eddie 3/21/2013 Examples: SUMDIV(12) returns 28. ** Thanks to Thomas Lake for pointing out that the variable "mat".idivis(N). CAS. For example. 12. DIM returns length as a number. SUMDIV(24) returns 60. 4. For strings.mdiv. For sequences or vectors." Not all CAS commands can be used in HP Prime programming at this time. or matrix. You may include code of what to do if the user presses Cancel. KILL Program: EXPORT TARGET() BEGIN LOCAL C:=0. 3. INPUT. N:=RANDINT(1. KILL. KILL HP Prime Program: TARGET. Block. list of "labels". 6. INPUT KILL: Terminates program execution. list of "help text") Note: Pressing Cancel will store a 0 in variable. :) WHILE. WHILE: Repeat a number of commands while a specific condition is test. INPUT(G. I hope you are ready for the intensity. INPUT. If you miss. Loop. Nothing dies. G:=1. the calculator gives you how may picks you needed to get the target number. "label". but it is not required. At the end of the game. Access: Cmds."GUESS:"."Guess?". 5. WHILE condition is true DO commands END. WHILE G?N DO C:=C+1. each with some new programming commands in this series. the calculator will tell you if number is higher and lower. On the HP Prime. Access: Tmplt. 1. GETKEY This tutorial is going to cover a lot. 5. TARGET is a game where you provide a guess to get a desired number.HP Prime Programming Tutorial #3: WHILE. I/O. the input can asked for more than one input. I promise.20). Press the soft key Check to check your work. Access: Tmplt. INPUT: Creates an input screen for variables. WHILE Caution: Watch your ENDs! Make sure an END is with each loop and the program itself. TARGET demonstrates INPUT with one prompt. IF G==0 THEN . "title". "title". 3. "help text") Multi-Variable: INPUT(list of variables."1 20"). One Variable: INPUT(variable. REPEAT. you can adjust the higher limit. END. 4. IF G > N THEN MSGBOX("Lower"). Concatenate EXPORT ULAM(N) BEGIN LOCAL C:=1. If n is odd. If n is even. REPEAT Featured: CONCAT(list1. END. Here is some thing for you to try with TARGET: 1. list2): Melds list1 and list2 into one. REPEAT IF FP(N/2)==0 THEN N:=N/2. END. L0:=CONCAT(L0. UNTIL N==1. multiply it by 3 and add 1. IF G < N THEN MSGBOX("Higher"). END. Loop. RETURN L0. Try it and of course. Can you display the list of guesses? REPEAT ULAM Algorithm: take an integer n. Math. ULAM counts how many steps it takes to get n to 1. Access: Toolbox. . OF STEPS="+C). MSGBOX("Correct! Score: "+C).KILL. Add a limited amount of guesses. 6. 6. END. REPEAT: Access: Tmplt. ELSE N:=3*N+1. List. C:=C+1. END. MSGBOX("NO. 2. L0:={N}. 3.{N}). divide it by 2. END. PRINT()."). the code returns to the terminal screen. GETKEY: Returns the key code of last key pressed. 8. GETKEY. 16. 8. 2. 34. 10. 4. 4. PRINT("Press Enter to exit. (Picture is from the HP Prime User's Guide) Access: Cmds. OF STEPS=6" List: {5. Which each key press. 5. . 16. 20. 26. The Prime's key map is below. REPEAT K:=GETKEY. 17. 11. The program terminates when the Enter key is pressed. 2. The Program KEYNO: The person presses key presses. OF STEPS=16" List: {22. 1} GETKEY The next section will introduce a super-important command. 13. 4. GETKEY EXPORT KEYNO() BEGIN LOCAL K. 40."). PRINT("Press any key to get its code. END. 6.Examples: ULAM(5) returns: Message Box: "NO. 1} ULAM(22) returns: Message Box: "NO. 52. IF K ? 0 THEN PRINT(K). We will be working with GETKEY over the entire series. I/O. Again. And until next time.UNTIL K==30. Example Key Codes: 33: 8 key 2: up 7: left 8: right 12: down 50: plus 45: minus This concludes Part 3. . it can't be said enough. END. thanks for all the comments and compliments. "Prompt". Use the IF THEN ELSE structure with INPUT to execute a set of default instructions if the user presses cancel.. First a tip from Han of the MoHPC Forum. Tip about INPUT Welcome to Part 4 of our programming series for the Prime.hpmuseum. major thanks to Han. store an empty string: var:=" ". Also includes a default scenario (optional). "Help". Thank you Han for allowing me to share this. INPUT(var. "title string". and 1 if a value is entered. Again. which is found at http://www.org/cgisys/cgiwrap/hpmuseum/forum. 1. CHOOSE and CASE CHOOSE: Creates a pop up choose box. "title string". default value) The type of variable maybe set to other than real numbers. INPUT returns a value of 0 if ESC or cancel is pressed.) THEN commands if values are entered ELSE commands if Cancel is pressed END. if you want var to be a string. ..cgi#255084. There are two syntaxes for CHOOSE: Simple Syntax (up to 14 options): CHOOSE(var. List syntax (infinite amount of items): CHOOSE(var. CASE IF test 1 THEN do if true END. Default values can be assigned to values as an optional fifth argument for INPUT. "item 2"}). For example. CHOOSE CASE: Allows for different test cases for one variable. I/O. Access: Cmds. . "item 1". Today's session will cover CHOOSE and CASE. "item n").. similar to what you see when you click on a soft menu. . {"item 1". Just remember to store such type before the INPUT command. choosing item 2 assigns the value of 2 to var. "Title". "item 2".HP Prime Programming Tutorial #4: CHOOSE and CASE. 6.. IF INPUT(. Choosing item 1 assigns the value of 1 to var. TERMVEL ."Type of Object".R.S.47.IF test 2 THEN do if true END.. 3.A. INPUT(R. RETURN T. DEFAULT commands END."Cube". MSGBOX("Terminal Velocity="+T). "Cylinder"."1."3. and Sectors EXPORT AREAC() BEGIN LOCAL C."Small Radius". 2. END."Areas". A:=?*(R^2S^2). CHOOSE(C."r="). Examples: Sphere.M. Sector"). L1:={1. M = .1. T:=?((2*M*L0(C))/(L1(C)*A*L2(K))).05.80665. Ring"."Surface Area"}).C."SI"."A="}.2 lb. IF C==2 THEN INPUT(S. CHOOSE(C.0028 m^2 Terminal Velocity: T = 24."Object".04}. IF C==3 .K..149821209 ft/s AREAC .6640475387 m/s Cube. CASE Let's look at two programs to demonstrate both CHOOSE and CASE.3403 ft^2 Terminal Velocity: T = 53.05 kg. END. L2:={.."Units".15.32."2. "Input Radius".Terminal Velocity of an Object EXPORT TERMVEL() BEGIN LOCAL L0:={9. Access: Cmds."Sphere".{"Mass".0765}.θ."TearShaped")."English").Area of Circles.A}.. M = 1.T. CHOOSE(K. . END. Circle". Branch. "R ="). CASE IF C==1 THEN A:=?*R^2. A = . INPUT({M. SI Units.174}.A.225. Rings.1. US Units. A = . {"M=". 5663706144 Sector: 2. r = 1.INPUT(θ.5. θ = π/4 radians or 45° Circle: 19. "Angle". It is so good to finally be rid of a cold and firing on all cylinders again. END. I thank you as always. \\ Assume you are in the correct angle mode IF HAngle==1 THEN \\ Test Angle Mode θ:=θ*?/180.6349540849 Ring: 12. MSGBOX("Area is "+A). Examples R = 2. RETURN A. A:=θ*R^2/2. .45436926062 That is how. END. "θ="). END. END.5. in general CHOOSE and CASE work.