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:
and to access them just do Varname.Property here. So for example if I have
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":
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!