HP Forums
forward declaration of variables? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: forward declaration of variables? (/thread-4928.html)



forward declaration of variables? - ji3m - 10-12-2015 01:28 AM

So to connect functions between files we use the forward declaration

myfun (); if my fun is in another file. This is good.

But this doesnt work with variables or pseudo constants

If myvar was in another file and we use it here, just hope the other was compiled first.

the declaration myvar; produces a new local to the current file.

This is wrong.

it should work the same way. If a local is desired it should be declared as
local myvar;

So the upshot is if variables are to be shared between files they better all be declared in
a file that is compiled before everyone else or an endless stream of errors occurs.

It seems like ppl is uninventing well understood rules of scope.


RE: forward declaration of variables? - Han - 10-12-2015 04:12 AM

(10-12-2015 01:28 AM)ji3m Wrote:  So to connect functions between files we use the forward declaration

myfun (); if my fun is in another file. This is good.

But this doesnt work with variables or pseudo constants

If myvar was in another file and we use it here, just hope the other was compiled first.

the declaration myvar; produces a new local to the current file.

This is wrong.

it should work the same way. If a local is desired it should be declared as
local myvar;

So the upshot is if variables are to be shared between files they better all be declared in
a file that is compiled before everyone else or an endless stream of errors occurs.

It seems like ppl is uninventing well understood rules of scope.

Here's how I understand the scope:

http://www.hpmuseum.org/forum/thread-215.html


RE: forward declaration of variables? - cyrille de brébisson - 10-12-2015 05:29 AM

Hello,

>It seems like ppl is uninventing well understood rules of scope.

No, all languages have their own rules for scope, and PPL has its own, slightly different, to adapt to the calculator case and system.

PPL recognizes 3 types of scope:
1) The system scope, which is anything that is already recognized by the system (ie, you can go to home or cas, type it, and it does something).
2) The current file or program scope (these are the same as 1 file is 1 program for PPL)
3) The current function scope

Notes:

1 can contain a number of USER defines objects, such as vars and functions from OTHER programs

2 At program scope level, you find program global variables. EXPORTED, or not (LOCAL) and program functions, once again, EXPORTED or not
Here LOCAL, if used means: Not exported. Probably a bad choice as local is also used in function.
Here you will also find forward declarations, but this is just a compilation trick to allow you to do double recursive functions. This is NOT designed to be like a C "function declaration".

3 at function scope, you find all the function local variables (including the parameters).


See, the PPL scope is well defined, BUT it is more complex that C or similar scope due to the fact that PPL programs execute, and are part of, the glogal calculator system.

Cyrille


RE: forward declaration of variables? - ji3m - 10-12-2015 12:59 PM

Thanks again. Once again ive been bitten by trying to develop a large app with small files. All my recent problems and questions are related to this. I realize now that it was a bad idea since it makes all my functions and variable global whick is not at all desireable. I got away with scope murder from day one since the order of compilation saved me. But now it is critical. I really did this since constantly copy /paste of a large file made it very hard to find errors. I should probably move to a single file now. BTW the android prime crashes frequentl and randomly but mosgly during compilation. It does it a lot when a program reference an external function which is not declared or a varible not already compiled. Dangling references?