[fpc-pascal] Declaring an array as var or not in function calls?
bo.berglund at gmail.com
Wed Apr 12 21:17:25 CEST 2023
I am wondering how to deal with this var or not in function calls:
In a processing program where I am building the command arguments for a write
operation by analyzing incoming commands and splitting the data such that the
write can be fully done by executing a series of smaller write commands for
chunks of data.
The container for this is a dynamic array of write commands and this array is
part of the arguments for the build function.
Like this (simplified):
TWriteBytesArgs = record
TWriteCommands = array of TWriteBytesArgs;
function BuildArgumentsArray(Arg: TWriteBytesArgs; var WArgs: TWriteCommands):
(Build arguments into WArgs)
Data: TBytes; //Container for actual data
- Fill Data array with values to write
- Set values to WCmd record members
if BuildArgumentsArray(WCmd, WCmds) then
for i := 0 to Length(WCmds) -1 do
WCmd[i].SourceAddr, WCmds[i]NumWriteBytes, Data);
In main() the Arg record is analyzed by a call to BuildArgumentsArray and based
on that one or several actual smaller write commands will be generated. These
are written to the WArgs array.
So the WArgs dyn array is originally length zero, but inside the
BuildArgumentsArray function its length will be set to contain the needed write
commands to do the complete write.
And the caller can iterate over the array items and generate a write command for
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...
It seems like it works also if I do not use the vars specification...
What is the deal here, is this kind of variable written to inside a called
function not needing to be set as var and it will work anyway?
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
Developer in Sweden
More information about the fpc-pascal