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

Sven Barth pascaldragon at googlemail.com
Thu Apr 13 14:08:10 CEST 2023

Bo Berglund via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am
Do., 13. Apr. 2023, 07:49:

> What is the use case for "out" compared to "var" when it seemingly is
> doing the
> same thing?
> Or is it exactly this:
> With "out" you get a pristine empty variable which you have to initialize
> if
> needed (setting its length in this case)?
> And you are ensured no stale data will be part of it?

When the compiler finds an "out" parameter in a call it does not need to
check whether the variable you pass is has been written to. With a "var"
parameter you'll get "variable not initialized" warnings/hints instead.
It's the task of the called function then to provide a correct value for
the parameter.

> >
> >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".
> So "out" it is unless I need to use the existing data or append new data
> to it,
> in which case I need "var", right?



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20230413/9bbd7983/attachment.htm>

More information about the fpc-pascal mailing list