[fpc-pascal] Converting old pascal written for Pascal/MT+ compiler
jacob.kroon at gmail.com
Tue Apr 4 09:43:43 CEST 2023
On 4/4/23 09:01, Tomas Hajny via fpc-pascal wrote:
> You don't need to change "var" to "const" - if you want to ensure the
> variables to persist in between the function/procedure runs, you need to
> move them to the global level, i.e. outside of the functions/procedures.
> It is not advisable as a general practice (to keep all variables
> globally), because then you might e.g. access the same variable from
> multiple functions by mistake, but it would probably solve your issue.
> Obviously, you might need to solve potential conflicts if you use the
> same variable names in multiple functions/procedures (e.g. by prepending
> the function/procedure name to the variable name or something like that).
I could move them to global scope, and add a prefix to avoid name
collisions, but using a "const" section seems more readable to me since
I don't need to add a prefix.
The wiki page says (https://wiki.freepascal.org/Const):
"The declaration const in a Pascal program is used to inform the
compiler that certain identifiers which are being declared are
constants, that is, they are initialized with a specific value at
compile time as opposed to a variable which is initialized at run time.
However, the default setting in Free Pascal is to allow const
identifiers to be re-assigned to."
Then it says there is a flag to make reassigning to a const variable an
error. But if I ignore that, it would seem this is just what I need,
since this seems to make the variable have a whole-program lifetime, no ?
What is the technical downside to using "const", or is it just cosmetic ?
More information about the fpc-pascal