Results 1 to 10 of 10

Thread: How to make a function return multiple results!

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default How to make a function return multiple results!

    How to make a function return multiple results!


    Short and simple


    Ever wanted to make a function return multiple results(aside from TVariant..)? Well here is a smple guide!

    Step 1) Create a record like this:

    Simba Code:
    type something = record
    end;

    Inside of that record you can put Integers, Strings, and well... any data type.. But that record represents them All! For this tutorial I will be using Finding a rock and it's location as an example. So here is my record:

    Simba Code:
    type Rock = record
      found:boolean;
      Location:TPoint;
    end;


    Ok so the record "Rock" has 2 properties: Found(boolean) and Location(TPoint). To access them make a variable like this:
    Simba Code:
    var
      Varname:Rock;
    and to access them just do Varname.Property here. So for example if I have
    Simba Code:
    var

    RockInfo:Rock

    and I want to acess Location I would do RockInfo.location. You can also have multiple variables:
    Simba Code:
    var
      a, b, c:Rock;

    Moving on,
    Now onto the function returning 2 results. This is fairly easy. First we make a function that returns "Rock":

    Simba Code:
    function FunctionName:Rock;
    begin
    end;

    then to make it return a property of the rock do

    Simba Code:
    result.propertyhere := blah;

    Example:

    Simba Code:
    function FindRock:Rock;
    begin
      result.found := true;
      result.location := Point(100, 100);
    end;

    Ways to use:

    Simba Code:
    type Rock = record
      found:boolean;
      Location:TPoint;
    end;

    function FindRock:Rock;
    begin
      result.found := true;
      result.location := Point(100, 100);
    end;

    begin
      Writeln(FindRock.found);
    end.

    Will print if found or not

    You can also assign variables like any other function:

    Simba Code:
    type Rock = record
      found:boolean;
      Location:TPoint;
    end;

    function FindRock:Rock;
    begin
      result.found := true;
      result.location := Point(100, 100);
    end;

    var
      FunctionInfo:Rock;

    begin
      FunctionInfo := FindRock;
      if (FunctionInfo.found = true) then
        Writeln('Wee');
    end.


    Hope this helped!

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Lol functions can still only return one value. You are returning "one record". Though the record holds multiplie fields.

    Don't forget the best way of getting multiple results from a function is pass by reference.. That way you don't create a temporary and then create a copy of that temp..


    In other words:

    Simba Code:
    type TRec = record
      x, y: Integer;
    end;

    Function GetMultiple: TRec;
    begin
      Result.X := 10;
      Result.Y := 10;
    end;

    Function GetMultipe: TRec;
    var
      Res: TRec;
    begin
      Res := [10, 10];
      Result := Res;
    end;

    Procedure GetMultiple(var Res: TRec);
    Begin
      Res.X := 10;
      Res.Y := 10;
    End;

    Guess which is the most efficient?
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    Lol functions can still only return one value. You are returning "one record". Though the record holds multiplie fields.

    Don't forget the best way of getting multiple results from a function is pass by reference.. That way you don't create a temporary and then create a copy of that temp..


    In other words:

    Simba Code:
    type TRec = record
      x, y: Integer;
    end;

    Function GetMultiple: TRec;
    begin
      Result.X := 10;
      Result.Y := 10;
    end;

    Function GetMultipe: TRec;
    var
      Res: TRec;
    begin
      Res := [10, 10];
      Result := Res;
    end;

    Procedure GetMultiple(var Res: TRec);
    Begin
      Res.X := 10;
      Res.Y := 10;
    End;

    Guess which is the most efficient?
    the last one because it's avoiding a step by auto assigning the var? lol

  4. #4
    Join Date
    Nov 2011
    Location
    United States
    Posts
    815
    Mentioned
    6 Post(s)
    Quoted
    284 Post(s)

    Default

    How to make a function return multiple results!
    How to Make and Use Records!




    I fixed your title.

  5. #5
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Simba Code:
    procedure MyMultipleProcedure(var x,y,z:integer);
    too mainstream?

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

    Default

    Quote Originally Posted by NKN View Post
    Simba Code:
    procedure MyMultipleProcedure(var x,y,z:integer);
    too mainstream?
    Technically the method does not return any results. It is merely that the arguments are passed as references and the method would edit the references.

  7. #7
    Join Date
    Sep 2008
    Posts
    754
    Mentioned
    8 Post(s)
    Quoted
    275 Post(s)

    Default

    Using Record :P nice guide

  8. #8
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Itankbots View Post
    How to make a function return multiple results!
    How to Make and Use Records!




    I fixed your title.
    Was not implying result as in the variable result.

  9. #9
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    Technically the method does not return any results. It is merely that the arguments are passed as references and the method would edit the references.
    Technically returning a record isn't returning multiple variables either. :'(

  10. #10
    Join Date
    Dec 2011
    Location
    New York, USA
    Posts
    1,242
    Mentioned
    12 Post(s)
    Quoted
    193 Post(s)

    Default

    you can have a function return an array of variable..

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
  •