Thursday, October 24, 2013

HP Prime Programming Tutorial #1: LOCAL, RETURN

Over 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.




Rules for Program Names:

1. Letters, numbers, and the underscore character (_) only.

2. The program name must start with a letter.


Structure of a HP Prime Program

A HPPP program is encased of an EXPORT - BEGIN - END structure. The layout is generally like this:

EXPORT program_name(arguments)
BEGIN
commands and comments go here
END;


Each line containing a command generally must end with a semicolon (;). A semicolon can by type by pressing ALPHA then the Plus key ( + ).

Comments can be typed. The are designated by two forward slashes. The slashes are typed by pressing the Divide key ( ÷ ). Anything in the line following the two slashes is ignored in running the program.


SQIN

Our first program is SQIN, because "Hello World" programs are so 2000s. SQIN takes a number, squares it, then calculates the reciprocal. In short we are defining a custom function:

SQIN(x) = 1/x^2

Commands:

RETURN: returns a result to the stack (home page). You can return numbers, lists, vectors, matrices, strings, or a combination of these times.
Access: Tmplt, 1. Block, 2. RETURN

All the program code in this tutorial series will be shown in Courier font.

EXPORT SQIN(X)
BEGIN
RETURN 1/X^2;
END;



Tip: You can check the syntax of the program just by pressing the Check soft key in the program editor. HP Prime will inform you if there is a syntax error and attempt to point you to the error. If there are no syntax errors, the Prime states "No errors in the program". I use the Check command all the time.


How to run the programs:

Home Mode - Textbook Entry,
Home Mode - Algebraic Entry,
CAS Mode:


Type the program name. Follow the name with parenthesis and enclose the required arguments.

Or use the Toolbox (top row of white keys, 2nd key from the left, it looks like a tool box), select the User touch key, select the program, and input the required arguments.

Home Mode - RPN Entry:

Enter each argument, separate each entry by pressing the Enter key. Type the name, and in the parenthesis state the number of arguments.

For example, if the program TEST has four arguments, the RPN stack would like this:

4: argument_1
3: argument_2
2: argument_3
1: argument_4
TEST(4) to run the program.


Examples to try with SQIN:

SQIN(5) returns .04
SQIN(36) returns .000771604938


The next program will demonstrate the concept of local variables.


MOPMT

LOCAL: Declares any variables to be local to the program. In other words, the variables are created, used, possibly displayed during program execution, and deleted at program termination.
Access: Tmplt, 4. Variable, 1. LOCAL


Tip: You can declare local variables and assign an initial value at the same time. For example: LOCAL K:=1; stores 1 in K and makes K a local variable.


MOPMT calculates the monthly payment of a loan. The arguments are: the loan amount (L), the interest rate (R), and the number of months (M).

EXPORT MOPMT(L,R,M)
BEGIN
LOCAL K:=R/1200;
K:=L*K/(1-(1+K)^-M);
RETURN "Payment ="+K;
END;



Tip: Use RETURN, TEXTOUT_P, and PRINT to return custom strings, which combine results, messages, and calculations. Parts are connected with a plus sign.


Examples:
MOPMT(4000, 9.5, 30) returns 150.317437565
MOPMT(370000, 3.5, 360) returns 1661.46534383

Try this and next time in the series I will highlight other things we can do with HPPP. Thanks!

Eddie

This blog is property of Edward Shore. 2013 









14 comments:

  1. Note: I accidentally published an "unfinished" version of the blog entry last night and removed it when I discovered this. I notice there was a question on the comments section and I would like to answer it here.

    "veganman has left a new comment on your post "HP Prime Programming Tutorial #1: LOCAL, RETURN":

    nice post, I haven't bought a prime yet, and I'm about to, but I think I'll just read your blog for a few before I do, so at least I'll know a few things before I get it.

    My main question at this time, is, is it worth upgrading from the 50g which I do know very well already..."

    My reply is:

    Veganman,

    Thank you.

    As you probably noticed, I put two duplicates of the same topic. One of the pages that wasn't quite finished.

    To answer your question, I have and use both the 50g and the Prime and like them both. The Prime has a few new features over the 50g: color screen, rechargeable battery, super fast processor, and two new graphing applications, one for geometry and one for implicit graphs (Advanced Graphing - my favorite app). The Prime adds additional advanced functions such as Si, Ci, Ei, Zeta, and the erf functions.

    The programming language is different, so there could be a learning curve since it is not RPL. Prime does have an RPN mode, though it is not as extensive as the 50g.

    Eddie

    My apologizes for any inconvenience.

    ReplyDelete
    Replies
    1. The 40G series also uses the same programming language. I think these powerful calculators have a serious handicap because of commercial decisions that make no sense.

      I think that 48G or 48GX was the last real calculator. HP calculators should do RPN and only do RPN! I stil use the 48SX and 48GX everyday.

      Delete
  2. Thanks again, Eddie. I am loving my new Prime. Your blog is always a huge help.

    ReplyDelete
  3. Home Mode - RPN Entry
    Do I need to define allways the number of used arguments to run the program, even if there is definition the number of variables (as in example SQIN) in "EXPORT SQIN(X)" and the program won't even work with bad number of them?

    How can I return to the stack more results from the program (for example in SQIN example RETURN 1/x^2; RETURN 1/x^3;)?

    ReplyDelete
    Replies
    1. Jouni,

      Answer to first question:
      You can have a program without arguments. I don't quite understand the second part of the first question. Did you mean the use of optional arguments?

      Answer to second question:
      For returning multiple results to the stack, I favor the use of a list of vector. Example:

      RETURN [1/x^2, 1/x^3];

      Multiple RETURN statements do not work well in my experience.

      Hope this helps,

      Eddie

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi!eddy could you write your post in spanish. It will be great. Please. Take care and thanks.

    ReplyDelete
  6. Thanks Eddie,
    You wonder why HP does not put such straight forward examples in their documentation.
    This is the best information about programming the HP Prime.

    ReplyDelete
  7. I read your post and need to thank you for sharing such pleasant lines. Be that as it may, I likewise need to instruct you to share more data as I had needed to peruse all the more yet didn't get acceptable substance from anyplace.
    integrated voice response

    ReplyDelete
    Replies
    1. I'll see what I can do. It is why I like to post programs: for documentation and anyone can learn from the examples.

      Delete
  8. I don't know why, but the program give me a syntax error where I use (,) instead (;) on the first line program of MOPMT.
    Is possible that is caused due the language settings that I am using portuguese? Or this is caused by RPN ou Algebraic Mode entry?

    ReplyDelete
    Replies
    1. In Settings, at "Digit Grouping" area choose as decimal symbol the "." instead of ",".
      For example: 123,456.789 or 123 456.789
      Of course after this you have to make changes to the programs you have written. "," instead of ";"

      Delete
  9. Hi Eddie
    Thank u very much for sorting our problems and may God bless u a million times my brother. Eddie could u pls help on HP Prime this time to get the value of K if K=M/b x d sq x Fcu
    M=200 kNM say 200x10 expnt 6
    b=300 mm
    d=675 mm
    Fcu= 30 N/mm sq
    The answer = 635.8mm

    ReplyDelete

How to Rotate Graphs

How to Rotate Graphs Introduction The key is to use parametric equations in our rotation.  Using the rotation angle θ, the rotatio...