SRL Archive - OSR Help

Passing a function result as parameter


riwu

2012-10-18 23:04:37

When I pass the result of a function as parameter of a procedure, will it evaluate the result and thereafter only use the result as the parameter of the procedure or will it go through the function again to retrieve the result each time the parameter is used in the procedure?

This may sound confusing so see my example:
DropArray(ClearTIAFromTIA(OreSearchSlots, gemSlots));
DropArray is a procedure which refers to its parameter in a loop, and ClearTIAFromTIA is a function that returns a TIA (which is passed to DropArray as parameter).

So when the procedure is called, will it:
1. Evaluates ClearTIAFromTIA and retrieve the result
2. Directly use the result thereafter as the parameter

OR

1. Go straight into DropArray
2. Retrieve the result of ClearTIAFromTIA whenever it uses the parameter, resulting in ClearTIAFromTIA being called multiple times.

In most cases it doesn't matter but in this case both the procedure and the function contains loops so it may affect performance. If it is the latter then i'd have to assign result of ClearTIAFromTIA to another variable.

Also would my ClearTIAfromTIA cause memory leak? (Not sure if resetting the result works?) I don't want to use wrapper since if for the case above, the formal is true i won't have to declare an extra variable every time i call the function.
function ClearTIAFromTIA(const TIA, ClearTIA: TIntegerArray): TIntegerArray;
var
l, l2, i, i2: Integer;
begin
SetLength(Result, 0); //resets the result to prevent leak
l:= High(TIA);
l2:= High(ClearTIA);
for i:=0 to l do
begin
for i2:=0 to l2 do
begin
if (ClearTIA[i2] = TIA[i]) then
Break;

if (i2 = l2) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)]:= TIA[i];
end;
end;
end;
end;

masterBB

2012-10-18 23:34:08

Only simba functions will have memory leaks when returning arrays as a Result. When written in pascalscript you don't have to worry about it. Lape fixed this issue.

function ClearTIAFromTIA(const TIA, ClearTIA: TIntegerArray): TIntegerArray;
var
l, c, i, i2: Integer;
begin
SetLength(Result, Length(TIA));
l:= High(TIA);
for i:=0 to l do
begin
if InIntArray(ClearTia, TIA[i]) then
continue;

Inc(c);
Result[c-1]:= TIA[i];
end;
SetLength(Result, c);
end;

riwu

2012-10-19 01:25:57

Thanks for the cleaner code. I figured out the other qns too.