Post Reply 
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:

#include <iostream>
using namespace std;
int main()
{
//Dati
    int a=2; int b=3;
//Risoluzione
    int c=a+b;
    int d=c*c;
    int f=c*c*c+d;
    cout<<"Risultato: "<<f<<endl;
    return 0;
}

Something that would not involve stack manipulation which would become rather difficult in longer programs.

Thanks in advance for your help.
Find all posts by this user
Quote this message in a reply
11-07-2015, 12:28 PM
Post: #2
RE: Local variables - C Style
(11-07-2015 09:08 AM)alen12345 Wrote:  Dear users,

I have not fully understood the scope of local variables. 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:

#include <iostream>
using namespace std;
int main()
{
//Dati
    int a=2; int b=3;
//Risoluzione
    int c=a+b;
    int d=c*c;
    int f=c*c*c+d;
    cout<<"Risultato: "<<f<<endl;
    return 0;
}

Something that would not involve stack manipulation which would become rather difficult in longer programs.

Thanks in advance for your help.

What calculator are you using?

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
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,

I have not fully understood the scope of local variables. 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:

#include <iostream>
using namespace std;
int main()
{
//Dati
    int a=2; int b=3;
//Risoluzione
    int c=a+b;
    int d=c*c;
    int f=c*c*c+d;
    cout<<"Risultato: "<<f<<endl;
    return 0;
}

Something that would not involve stack manipulation which would become rather difficult in longer programs.

Thanks in advance for your help.

What calculator are you using?

Sorry about that, I'm on a HP50G; I have also edited my first post.
Find all posts by this user
Quote this message in a reply
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:

<<  2. 3.  -> A B          @@create and load local A with 2. and B with 3.
       <<                  @@between this brackets the locals A and B exist
            5. 'A' STO     @@overwrite contents of A with 5.
            A B            @@put value of A on stack 2 and value of B on stack 1
       >>                  @@between this brackets the locals A and B exist 
A                          @@outside the scope, the program returns only 'A'
                           @@as an variable name (as an algebraic term), with no physical
                           @@store place 
>>

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
Find all posts by this user
Quote this message in a reply
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:

« 2 3                         ; Dati
  0 0 0 ->  a b c d f         ; a=2; b=3; c=0; d=0; f=0
                              ; Risoluzione
  « a b + 'c' STO             ; c=a+b
      c SQ 'd' STO            ; d=c*c 
      'c*c*c+d' EVAL 'f' STO  ; f=c*c*c+d
      f "Risultato" ->TAG     ; cout<<"Risultato: "<<f<<endl                              
  » 
»

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.
Find all posts by this user
Quote this message in a reply
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:  [...]
If you want to use local variables c, d and f in the inner structure, then you have to create and initialize them beforehand. [...]

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
                               ; Risoluzione 
« → a b                                          @ The data are read from the stack
  « a b + → c                  ; c=a+b           @ First level structure define c
    « c SQ → d                 ; d=c*c           @ Second level struct.  define d
      « c c c * * d + → f      ; f=c*c*c+d       @ Third level struct.   define f
        « "Risultato" f →STR +                   @ and use it to display trace message on 1st line
          1 DISP               ; cout<<"Risultato: "<<f<<endl                              
        »                                        @ here f is unknow but d c a & b are defined
      »                                          @ here d is unknow but c a & b are defined
    »                                            @ here c is lost but a & b still ok
  0 »                         ; return 0         @ Return value 0 to the stack
»                                                @ here none of the local variable are know

Of course, a much more way of doing it in RPL is samething like :
Code:

2 3  « + "Risultato" OVER SQ ROT OVER * + →STR + 1 DISP 0 »
↑ ↑     ↑                   ↑            ↑
│ │     │                   │            │
a b    c=a+b               d=c*c        f=c*c*c+d

... 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 » »
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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:
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.

I second this!
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)