Liz Hundy wrote:
> Yes, I would do as you suggest. Pass grpNodeCount as an extra parameter to
> the function.
> An alternative is to nest the whole recursive procedure inside another
> procedure that has grpNodeCount as a local variable. This may be quicker as
> it saves passing the value in at each call to the function. Maybe one of the
> gurus out there might like to comment on this.

It saves passing the value, but it passes the address of the caller's 
stack frame instead. There's a fair amount of magic to ensure that the 
nested function has access to its parent's variables. I really don't 
want to think about how convoluted that can get when the nested function 
is recursive.

Keep in mind that the first three parameters of any function get passed 
in registers. The difference between calling a two-argument function and 
a three-argument function is negligible.

If I wanted to preserve the two-argument interface for the caller, then 
I'd make a three-argument nested function:

procedure tsGrpExtractNodes(grp: tsxGROUP; var nodes: PObjArray);
     procedure ExtractNodesWorker(grp: tsxGROUP; var nodes: PObjArray;
       var grpNodeCount: Cardinal);
     begin
       // ...
     end;
var
   Count: Cardinal;
begin
   Count := 0;
   ExtractNodesWorker(grp, nodes, Count);
   SetLength(nodes, Count);
end;

The difference is that the nested function no longer accesses the 
parent's stack, and so the compiler generates it just as it does a 
regular non-nested function. It's just that the scope of the nested 
function is limited to the parent function. The parent function 
allocates the third parameter and initiates the recursive call. The 
nested function should then call itself (not its parent).

-- 
Rob
__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to