Local variables - C Style
|
11-07-2015, 09:08 AM
(This post was last modified: 11-07-2015 12:47 PM by alen12345.)
Post: #1
|
|||
|
|||
Local variables - C Style
Dear users,
I have not fully understood the scope of local variables on my HP50G. I would like to create programs on the fly which store intermediate result in order to display them just before the end of the program. Something stupid like this one in C++: Code:
Something that would not involve stack manipulation which would become rather difficult in longer programs. Thanks in advance for your help. |
|||
11-07-2015, 12:28 PM
Post: #2
|
|||
|
|||
RE: Local variables - C Style
(11-07-2015 09:08 AM)alen12345 Wrote: Dear users, What calculator are you using? Graph 3D | QPI | SolveSys |
|||
11-07-2015, 12:48 PM
Post: #3
|
|||
|
|||
RE: Local variables - C Style
(11-07-2015 12:28 PM)Han Wrote:(11-07-2015 09:08 AM)alen12345 Wrote: Dear users, Sorry about that, I'm on a HP50G; I have also edited my first post. |
|||
11-07-2015, 01:41 PM
(This post was last modified: 11-07-2015 01:45 PM by peacecalc.)
Post: #4
|
|||
|
|||
RE: Local variables - C Style
Hello alen12345,
I don't understand c++, but the scope of local variables are easy with the hp 50g. You have three places where you can store values of any kind. a) stack b) global variables c) local variables Every main program is enclosed between "<<" ">>" . If a nested pair of "<<" ">>" exist, it is the opertunity to create a local variables, have a short look: Code:
The recalling above involved the stack of course, but normally you will do something with the value, for example to feed another function, so the stack becomes empty like before. Maybe interesting for you: if you use locals in a loop and you interrupt with HALT, you can load the local variables with new values (with STO) or you can recall their values (with RCL), but you must have the local variable names in mind (good for debugging) because these names doesn't appear if you press VAR on your calc. greetings peacecalc |
|||
11-07-2015, 02:06 PM
(This post was last modified: 11-07-2015 02:29 PM by Gerson W. Barbosa.)
Post: #5
|
|||
|
|||
RE: Local variables - C Style
Ciao Alen12345,
The following is a close translation of your c++ program: Code:
If you want to use local variables c, d and f in the inner structure, then you have to create and initialize them beforehand. Peacecalc's program above makes more sense in RPL, but for longer and more complicated programs a mix of stack operations, local variables and algebraic expressions might be easier to accomplish. Regards, Gerson. |
|||
11-08-2015, 02:52 PM
(This post was last modified: 11-08-2015 03:37 PM by C.Ret.)
Post: #6
|
|||
|
|||
RE: Local variables - C Style
Hi
(11-07-2015 02:06 PM)Gerson W. Barbosa Wrote: [...] Using the STO command on local variable looks strange to me. In RPL a more natural way of doing this is to use an embedded structure, such as : Code: 2 3 ; Dati @ Data are generally out of the program Of course, a much more way of doing it in RPL is samething like : Code:
... but , you may object that this RPL-style way use and abuse stack movements, making it difficult to trace. That the main reason of local varaible, making the code more readible or self explainable. Such as: Code: « → height radius « ∏ →NUM radius SQ * height * "Vol_Cylin" →TAG » » |
|||
11-08-2015, 05:29 PM
Post: #7
|
|||
|
|||
RE: Local variables - C Style
(11-08-2015 02:52 PM)C.Ret Wrote: Using the STO command on local variable looks strange to me. 1+ It makes reasoning about a program easier if you know that something is a value and not a variable. I tend to use local variables to remove values from the stack that are used later but are currently in the way. Usually this avoids complicated stack operations. What's wrong with: Code: « + DUP SQ OVER + * » Cheers Thomas |
|||
11-08-2015, 07:28 PM
(This post was last modified: 11-08-2015 07:29 PM by peacecalc.)
Post: #8
|
|||
|
|||
RE: Local variables - C Style
Hello all,
Quote:original from Thomas Klemm: I second this! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)