[fpc-pascal] Declaring an array as var or not in function calls?

Sven Barth pascaldragon at googlemail.com
Wed Apr 12 23:59:02 CEST 2023


Giuliano Colla via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Mi., 12. Apr. 2023, 22:54:

> Il 12/04/2023 21:17, Bo Berglund via fpc-pascal ha scritto:
>
> 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?
>
> Your Wargs is defined as an open array, i.e. its length is unknown at
> compile time. Therefore the compiler cannot pass it in the subroutine call
> other than a pointer to the array. It cannot pass the array content because
> it doesn't know its length.
> In that case you may omit the var specifier.
> But if one day you change your mind, and decide to establish a size for
> your array, without var the compiler may decide to pass the full array data
> to the procedure, and therefore whatever you do in your procedure is done
> on the local copy of the array and not in the original array.
> My suggestion is to specify "var" whenever your procedure is expected to
> modify the data passed. It makes the program more readable, even if "var"
> is redundant.
>

There is *no* open array in the example given. Also even if there were
changing the length of an open array would not work...

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230412/d249b5af/attachment.htm>


More information about the fpc-pascal mailing list