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:
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:
Ok so the record "Rock" has 2 properties: Found(boolean) and Location(TPoint). To access them make a variable like this:
and I want to acess Location I would do RockInfo.location. You can also have multiple variables:
Moving on,
Now onto the function returning 2 results. This is fairly easy. First we make a function that returns "Rock":
then to make it return a property of the rock do
Example:
Ways to use:
Will print if found or not
You can also assign variables like any other function:
Hope this helped!
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!