Store and reuse intermediate variable
|
12-05-2022, 09:10 AM
Post: #1
|
|||
|
|||
Store and reuse intermediate variable
Hi all.
One year ago I wrote a post related to subroutine, I'm here again becasue I had a turbulent year. Some years ago I bought a HP 50g and I'm trying to program it with litte improvement ... My problem is using local variable in a program, in detail store an intermediate value and reuse it. To have practice I wrote a simple program that calculate mortgage installment (even if there are financial tools that alreade did it). I would calculate the installment and the total amount paid (simply multiplying this intermediate value for number of years and months). My problem is store this value. A sample of program is: Code:
I red and checked tons of manuals but in didn't find some example that help me. Thanks in advance. |
|||
12-05-2022, 06:36 PM
Post: #2
|
|||
|
|||
RE: Store and reuse intermediate variable
I make a variant of program, it use global variable ...
Code:
In this way I can obtain what I need, but in this way global variables are used, a practice not recommended by many. How I can avoid use global variables? |
|||
12-05-2022, 10:22 PM
Post: #3
|
|||
|
|||
RE: Store and reuse intermediate variable
You should init R when initializing the other local vars, then you can simply ‘R’ STO and it will not create a global (HOME) var.
Example: Code:
Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co |
|||
12-06-2022, 07:13 AM
Post: #4
|
|||
|
|||
RE: Store and reuse intermediate variable
Hi Pinkman.
Thanks for answer. I'm trying to avoid global variables as many people suggest. Thanks |
|||
12-07-2022, 05:48 AM
Post: #5
|
|||
|
|||
RE: Store and reuse intermediate variable
(12-06-2022 07:13 AM)oierpa Wrote: Hi Pinkman. So do I. Did you try my suggestion? Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co |
|||
12-07-2022, 02:19 PM
Post: #6
|
|||
|
|||
RE: Store and reuse intermediate variable
Hi Oierpa,
What Pinkman is suggesting is that you do this: Code: << Note that during the INPUT, if the user recalls 'R' then they get the local zero value. |
|||
12-07-2022, 08:35 PM
Post: #7
|
|||
|
|||
RE: Store and reuse intermediate variable
Thank you BruceH!
Yes, once a local var is declared, you can update its value with the same syntax than with global vars. Look at line 5: adding a 0 to be stored in local var R Look at line 6: R is appended Code:
Thibault - not collector but in love with the few HP models I own - Also musician : http://walruspark.co |
|||
12-09-2022, 06:41 PM
Post: #8
|
|||
|
|||
RE: Store and reuse intermediate variable
Hi all.
Excuse me for delay, I was out for some days for my job. I just tested program with your suggestions and it works! Thanks a lot! So in the future, if I have other variabiles (more than one) I've to inizialize with a dummy value, right? I would to convert some program in RPL. Thanks again! |
|||
12-12-2022, 03:16 PM
Post: #9
|
|||
|
|||
RE: Store and reuse intermediate variable
Hi all.
I just make a small variant to my simple program, in order to be general, here the code: Code:
In this version of program, I've the problem that, even if I wrote -> NUM, I can see an algebraic result (the formula) and not the results number. I don't understood why, so after some tests I modified in this version; Code:
Why if in the exponent I keep 'n*A' indeed P (=n*A) doesn't work? Actually I don't need to display also "P" during calculation, in other programs, if I have to calculate intermediate values, is not important see the value on the stack. Thanks in advance. |
|||
12-20-2022, 07:14 AM
Post: #10
|
|||
|
|||
RE: Store and reuse intermediate variable
You could also just duplicate the value with DUP before creating the local variable R:
Code: « "Ins C t A n" { Just noted that you are now using → NUM instead of →NUM in your listing. That could be the reason why you got an algebraic result. |
|||
12-20-2022, 08:48 AM
Post: #11
|
|||
|
|||
RE: Store and reuse intermediate variable
Thanks Thomas.
It was my error writing "-> NUM" indeed of "->NUM". Thanks also for "DUP" suggestion. Merry Christmas to everyone! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)