[fpc-pascal] Declaring an array as var or not in function calls?
Giuliano Colla
giuliano.colla at fastwebnet.it
Wed Apr 12 22:54:44 CEST 2023
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.
Giuliano
--
Do not do to others as you would have them do to you.They might have different tastes.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230412/fd4a6400/attachment.htm>
More information about the fpc-pascal
mailing list