Store and reuse intermediate variable - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: General Forum (/forum-4.html) +--- Thread: Store and reuse intermediate variable (/thread-19256.html) |
Store and reuse intermediate variable - oierpa - 12-05-2022 09:10 AM 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. RE: Store and reuse intermediate variable - oierpa - 12-05-2022 06:36 PM 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? RE: Store and reuse intermediate variable - pinkman - 12-05-2022 10:22 PM 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:
RE: Store and reuse intermediate variable - oierpa - 12-06-2022 07:13 AM Hi Pinkman. Thanks for answer. I'm trying to avoid global variables as many people suggest. Thanks RE: Store and reuse intermediate variable - pinkman - 12-07-2022 05:48 AM (12-06-2022 07:13 AM)oierpa Wrote: Hi Pinkman. So do I. Did you try my suggestion? RE: Store and reuse intermediate variable - BruceH - 12-07-2022 02:19 PM 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. RE: Store and reuse intermediate variable - pinkman - 12-07-2022 08:35 PM 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:
RE: Store and reuse intermediate variable - oierpa - 12-09-2022 06:41 PM 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! RE: Store and reuse intermediate variable - oierpa - 12-12-2022 03:16 PM 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. RE: Store and reuse intermediate variable - Thomas Klemm - 12-20-2022 07:14 AM 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. RE: Store and reuse intermediate variable - oierpa - 12-20-2022 08:48 AM Thanks Thomas. It was my error writing "-> NUM" indeed of "->NUM". Thanks also for "DUP" suggestion. Merry Christmas to everyone! |