Results 1 to 3 of 3

Thread: Passing a function result as parameter

  1. #1
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default Passing a function result as parameter

    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:
    Simba Code:
    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.
    Simba Code:
    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;

  2. #2
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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.

    Simba Code:
    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;
    Working on: Tithe Farmer

  3. #3
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •