[fpc-pascal] Declaring an array as var or not in function calls?
Sven Barth
pascaldragon at googlemail.com
Thu Apr 13 00:06:16 CEST 2023
Bo Berglund via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mi., 12. Apr. 2023, 21:17:
> Now my question:
> ----------------
> Since I am modifying the WArgs variable inside the BuildArgumentsArray
> function
> and using its new content afterwards in the WriteItem function I assumed
> that I
> had to use var as shown above, but I am not certain this is the case...
>
If you modify the length or the contents of a dynamic array parameter then
you need to use "var" or "out" modifiers (the later only if you don't need
the data passed in).
> It seems like it works also if I do not use the vars specification...
>
It should not. If it does it will likely be an implementation detail and
you shall not rely on it. Do you have an explicit example for this
behavior?
And if so, what is the rule for *when* to use var in function call argument
> lists for items that will be changed inside the called function and used
> afterwards
>
The rule is simple: if you need to modify the parameter value itself (e.g.
changing a primitive type, changing a field in a record, changing an
array's length or data, changing the value of a class instance - but not
its fields!) you need to pass it as "var". If it's a write only parameter
you can even use "out".
Regards,
Sven
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230413/b9abf1eb/attachment.htm>
More information about the fpc-pascal
mailing list