SRL Archive - OSR Beginner Scripting Tutorials

How to make a function return multiple results!


rj

2013-09-09 17:58:18

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:

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:

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:

var
Varname:Rock;
and to access them just do Varname.Property here. So for example if I have

var

RockInfo:Rock

and I want to acess Location I would do RockInfo.location. You can also have multiple variables:
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":

function FunctionName:Rock;
begin
end;

then to make it return a property of the rock do


result.propertyhere := blah;

Example:

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

Ways to use:

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:

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!

Brandon

2013-09-09 19:14:49

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:


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?

rj

2013-09-09 19:34:33

From: Brandon
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:


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

Itankbots

2013-09-09 21:29:59

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




I fixed your title.

NKN

2013-09-10 07:06:29


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

riwu

2013-09-10 07:38:10

From: NKN

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.

samerdl

2013-09-10 09:13:12

Using Record :P nice guide

rj

2013-09-10 17:33:11

From: Itankbots
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.

NKN

2013-09-10 18:49:25

From: riwu
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. :'(

Nebula

2013-09-10 19:00:35

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