I'm not positive, because I've never used them, but I'm pretty sure variables configured as const, can't be changed once they're defined.  If that's your intent, then feel free to use them, but it sounds like you're trying to make variables (not constant values) last for the whole program, with manipulation being allowed.  In that, case, simply define them in the var section before you do any of the functions and procedures.  That will make them visible for the whole program, and they won't go out of scope.  I know many folks contend that global variables should be kept to a minimum, and scope should be carefully evaluated, and only allow variables to be usable when they're actually needed, and to some degree, I agree, but there's times when it's just too darned convenient to have something accessible for the whole program over multiple functions/procedures, and in those cases, I see nothing wrong with having as many globals as you need.

Often, when I'm doing a quick and dirty program, (I.E. one to change a data file from one format to another), I won't even bother with functions and procedures, just write the whole program as one big main procedure, since it's only being run once or twice, then never used again, I see nothing wrong with this approach either, and in that case, all variables are global.

No reason you can't do the same, and not depend on quirks of the compiler to keep your items in scope especially when they clearly shouldn't be.



Am 04.04.2023 um 08:16 schrieb Jacob Kroon via fpc-pascal:

I was able to write a couple of perl-scripts that are able to convert my old Pascal sources to something that fpc can parse. Amongst other things, the scripts inject the "public name"/"external name" annotations so that the program can link.

But I suspect I have a new problem: With the old Pascal/MT+ compiler it would appear that local variables declared in functions/procedures have a life-time that spans the whole program, like a "static" declared variable in C. With fpc, it looks like locally declared variables are automatic, put on the stack(?), and so they go out of existence once out of scope ?

The program depends on this feature in the old compiler. I did some googling and found that putting local variables in a "const" section instead of "var" would make them have a "whole-program" lifetime, but then I need to provide them with an initial value.

Do I have any other option besides changing from "var" to "const" everywhere, and provide initial values in all declarations ?

Regards
Jacob

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to