Hello,
I'm currently RTFM Programmer's Reference and getting acquainted with FPC. On page 198, the following program appears to illustrate the use of out vs var.
What is interesting is that the code that is generated to pass an out parameter is different than the code generated to pass a var parameter.
{$H-}
program OutParameters;
Procedure DoA(var A : integer);
begin
A := 2;
writeln(A);
end;
procedure DoB(out B : integer);
begin
B := 3;
writeln(B);
end;
var
C, D : integer;
begin
DoA(C);
DoB(D);
end.
In the case of the var parameter, the address of the parameter is first loaded into rax then moved into rcx as shown below (debugger assembly view) whereas in the case of the out parameter, the address is loaded directly into rcx.
OutParameters.lpr:21 DoA(C);
000000010000150A 488d05efda0000 lea 0xdaef(%rip),%rax
0000000100001511 4889c1 mov %rax,%rcx
0000000100001514 e847ffffff callq 0x100001460 <DOA>
In the case of the out parameter, the address of the parameter is directly
loaded into rcx.
OutParameters.lpr:22 DoB(D);
0000000100001519 488d0df0da0000 lea 0xdaf0(%rip),%rcx
0000000100001520 e88bffffff callq 0x1000014b0 <DOB>
It seems that when using a var parameter, the address of the parameter is not passed optimally, yet the compiler knows how to generate optimal code since it does for the out parameter.
Note: The above code is the result of compiling with -03 optimizations.