Result of recursive function with pascal

时间:2018-03-25 18:44:45

标签: delphi

I have the next recursive function and I´d like return the sum of Height of the controls with Tag = 1. How get the sum without use global var when using recursive function?

procedure getSumHeightTagOne;
var hh:double;
begin
  hh:=ListChildren(myObj,0); ->Here I´d like to get the sum
end;

function ListChildren(Obj : TFMXObject; Level : Integer):double;
var  i: Integer;
begin
  for i := 0 to Obj.ChildrenCount-1 do begin
    if Obj.Children[i].Tag=1 then begin
      //add height ->  hh:=hh+Obj.Children[i].height
    end;
    ListChildren(Obj.Children[i],Level+1);
  end;
end;

1 个答案:

答案 0 :(得分:4)

Use the Result variable which acts as the function return parameter:

function ListChildren(Obj : TFMXObject; Level : Integer):double;
var  i: Integer;
begin
 Result := 0;
  for i := 0 to Obj.ChildrenCount-1 do begin
    if Obj.Children[i].Tag=1 then begin
      //add height ->  
      Result:=Result+Obj.Children[i].height
    end;
    Result := Result + ListChildren(Obj.Children[i],Level+1);
  end;
end;