<div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Tomas Hajny via fpc-pascal <<a href="mailto:fpc-pascal@lists.freepascal.org">fpc-pascal@lists.freepascal.org</a>> schrieb am Di., 4. Apr. 2023, 09:51:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 2023-04-04 09:43, Jacob Kroon wrote:<br>
<br>
<br>
Hi Jacob,<br>
<br>
>> You don't need to change "var" to "const" - if you want to ensure the <br>
>> variables to persist in between the function/procedure runs, you need <br>
>> to move them to the global level, i.e. outside of the <br>
>> functions/procedures. It is not advisable as a general practice (to <br>
>> keep all variables globally), because then you might e.g. access the <br>
>> same variable from multiple functions by mistake, but it would <br>
>> probably solve your issue. Obviously, you might need to solve <br>
>> potential conflicts if you use the same variable names in multiple <br>
>> functions/procedures (e.g. by prepending the function/procedure name <br>
>> to the variable name or something like that).<br>
>> <br>
> <br>
> I could move them to global scope, and add a prefix to avoid name<br>
> collisions, but using a "const" section seems more readable to me<br>
> since I don't need to add a prefix.<br>
> <br>
> The wiki page says (<a href="https://wiki.freepascal.org/Const" rel="noreferrer noreferrer" target="_blank">https://wiki.freepascal.org/Const</a>):<br>
> <br>
> "The declaration const in a Pascal program is used to inform the<br>
> compiler that certain identifiers which are being declared are<br>
> constants, that is, they are initialized with a specific value at<br>
> compile time as opposed to a variable which is initialized at run<br>
> time.<br>
> <br>
> However, the default setting in Free Pascal is to allow const<br>
> identifiers to be re-assigned to."<br>
> <br>
> Then it says there is a flag to make reassigning to a const variable<br>
> an error. But if I ignore that, it would seem this is just what I<br>
> need, since this seems to make the variable have a whole-program<br>
> lifetime, no ?<br>
> <br>
> What is the technical downside to using "const", or is it just cosmetic <br>
> ?<br>
<br>
If you read the documentation (wiki or the real documentation in <br>
PDF/HTML etc.) properly, you don't find there anything saying that <br>
constants declared locally within functions or procedures are guaranteed <br>
to keep their modified values across runs of that function/procedure. It <br>
might behave like that, but you shouldn't rely on it unless it's a const <br>
on a global level (and then renaming may still be needed in case of <br>
conflicts).<br></blockquote></div></div><div dir="auto"><br></div><div dir="auto">It *is* documented (the remark at the end): <a href="https://www.freepascal.org/docs-html/current/ref/refse10.html#x22-210002.2">https://www.freepascal.org/docs-html/current/ref/refse10.html#x22-210002.2</a> </div><div dir="auto"><br></div><div dir="auto">Regards, </div><div dir="auto">Sven </div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>