Post Reply 
Local variables - C Style
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
Post Reply 


Messages In This Thread
Local variables - C Style - alen12345 - 11-07-2015, 09:08 AM
RE: Local variables - C Style - Han - 11-07-2015, 12:28 PM
RE: Local variables - C Style - alen12345 - 11-07-2015, 12:48 PM
RE: Local variables - C Style - peacecalc - 11-07-2015, 01:41 PM
RE: Local variables - C Style - C.Ret - 11-08-2015 02:52 PM
RE: Local variables - C Style - peacecalc - 11-08-2015, 07:28 PM



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