Bo Berglund via fpc-pascal <fpc-pascal@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?
>

Correct.

Regards,
Sven

>
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to