Making and understanding Functions with Multiple Parameters
This Tutorial is based on you already knowing how normal Procedures/Functions work.
First off what type of Parameters can we use in our functions?
Originally Posted by
nielsie95
It's also useful for reading/understanding the parameters. If you use var, you would assume it's also used for input.
const -> input + should not be changed inside the function
nothing -> similar to const, but can be changed inside the function
var -> input/output
out -> output (similar to Result)
For this tutorial we will only use the four main data types used in SRL.
Simba Code:
B : Boolean;
I : Integer;
E : Extended;
S : String;
Variables (Input/Output)
A variable is an identifier (a name, if you will) that we assign to a specific memory location whose content can be altered. Variables can be defined as being of a standard or user-defined data type. They can be declared in 3 places. Global Variables in the beginning of the Program. Using a Variable as a Parameter of the Procedure/Function. Finally Local Variables in the beginning of the Procedure/Function.
Simba Code:
Program Variables;
Var //Global Variables can be used anywhere in the program.
MyHouseX, MyHouseY : Integer;
Procedure GetHome(var x, y : integer);// Parameter Variables
var
A, b : integer; //Local Variables
begin
A := 400;
B := 400;
MoveMouse(A + Random(5), B + Random(5));
GetMousePos(x, y);
end;
function WhereIsMyHouse(var x, y : integer): Boolean;
begin
Movemouse(x + 20 ,y + 20);
Wait(1000);
GetMousePos(x, y);
If x > 410 then
Result := True
end;
begin
GetHome(MyHouseX, MyHouseY);
Wait(400);
if WhereIsMyHouse(MyHouseX, MyHouseY) then
begin
writeln(inttostr(MyHouseX) + ' is the X coordinate of my House');
writeln(inttostr(MyHouseY) + ' Is the Y coordinate of my House');
end;
end.
Constants (Input Only)
We’ll start at the top with Const or Constant. As you can guess Constants (unlike Variables) cannot be changed while the program is running. There are two types of constants that exist Global and Local Constants. Global constants are define at the beginning of the script after the Program Title. Local Constants are defined in the Parameters of the Function or Procedure. Constants are used to Input data to be used. The only time you have to declare a constants type is when used in the Parameters of a Function.
You can put this into Scar/Simba and play around with it.
Simba Code:
Program Constants;
Const
MyConst = 'Brak';
Procedure Write_name;
Begin
Writeln(MyConst);
End;
// Local Const used with a Function
Function Write_My_Name(const MyName : String) : String;
Begin
Writeln(MyName);
Wait(500);
Result := ('Really My name is...' );
End;
// Declaring it in the function Parameters allows another
//Procedure/Function to input the Constant that is to be used;
Begin // Mainloop
Writeln(Write_My_Name(MyConst));
Wait(1000);
Write_Name;
End.
Out (Output)
Out should only be used in place of variable in Function/Procedure Parameters. The purpose of an Out parameter is to pass back value's to the calling Procedure/Function. The inital value of the Out is discarded upon entry into the Function/Procedure and should not be used. If a variable must be used to pass a value to a function and retrieve data from the function, then a variable parameter must be used. If only a value must be retrieved, a out parameter can be used. Default Values are not supported by Out Parameter.
Here is a Nifty Example that you can put this into Scar/Simba and play around with.
Simba Code:
program Funny;
var
Person : TStringArray;
Isfunny : boolean;
Who : String;
procedure Laugh(Const Name : String; out funny: boolean);
begin
Case Name of
'Mixster', 'Benland' : Funny := true;
'Jason2GS', 'Robot1' : Funny := False;
end;
end;
Begin
Person := ['Benland','Mixster','Jason2GS','Robot1'];
Who := Person[Random(4)];
Laugh(Who, isfunny);
Writeln(Who + ' is funny = ' + BoolToStr(Isfunny));
end.
Now for the one we see all the time, But don't really realize what it does.
Nothing(Input)
Quite simply put mean not declaring what the parameter is.
Example
It is used in the same way as Constant except that it can be changed during the Procedure/Function. Check the example Below:
Simba Code:
Program WhoDidIt;
Var
A, B, C, I, Person: Integer;
Procedure PresentEvidence(D, E, F : integer; Out guilt : integer);
begin
If D > 0 then
D := 1;
if E > 2 Then
E := 1;
If F < 5 then
F := 1;
Guilt:= D + E + F;
end;
begin
for I := 1 to 3 do
begin
A := Random(2);
B := Random(4);
c := Random(10);
PresentEvidence(A, B, C, Person);
if person = 3 then
Writeln('Person #' + Inttostr(I) + ' was Guilty.')
else
if person = 2 then
Writeln('Person #' + Inttostr(I) + ' is very suspious.');
end;
end.
Recap with Nielsie95
Originally Posted by
nielsie95
It's also useful for reading/understanding the parameters. If you use var, you would assume it's also used for input.
const -> input + should not be changed inside the function
nothing -> similar to const, but can be changed inside the function
var -> input/output
out -> output (similar to Result)
If you understood what Nielsie95 was saying this time. Then it's time to move on to practical application. Using those parameters to make your own custom Procedures and Function's
----------------------------------------------------------------------------------------------------------------------------------------------------
Practical Application
Input
You use your data to affect the outcome.
Simba Code:
DebugMode = true;
Procedure Debug(const Input : String;); //This is where you input your data
begin
if DebugMode then // It will do the next step Base on this
Writeln(Input); // Here is where your Input was used
end;
Simple Right. Next...
Export/Return
This is where you have the Function return Data.
Simba Code:
Const
DebugMode = true;
Function Debug(Input : String;) : Boolean; //With Boolean added to the end its Exporting a Variable.
begin
if DebugMode then // It will do the next step Base on this
Writeln(Input);// Here is where your Input was Used
else
Result:= false; //Result sends the Variable to the export on the outside of the ().
end;
It Returns a Variable. In this case a Boolean.
Now for the fun.
Exporting/Returning Multiple Variables
To do this you must declare the Variable's that you are exporting/returning. Normally you'd think to put:
But that only allows the procedure to use those Variables. Declaring the Variables inside the () let's the script know that you want to return that data. Such as:
Simba Code:
Function DidItHappenWhereAt(var x, y : Integer;): Boolean;//Remember declare it as a Var means input and output.
Notice that it looks just like normal except inside the (). For multiple different variables to return from inside the () though it's a little different.
Simba Code:
Function DidItHappenWhereWhen(var x, y : Integer; var Really : Boolean) : Extended;
- When using multiple Variable types you don't put a , after them.
- If your Returning value is the last Variable in the (). Then you leave off the ; at the end.
- In order to use the returning variables you must assign them a Variable of the same type inside the procedure or function you are using.
Putting what you've learned to work
You can copy and paste this into your Scar or Simba to see how it really works.
Simba Code:
program MultiVarReturn;
Function DidItHappenWhereWhen(var x, y : Integer; out Really : Boolean) : Extended;
begin
If Random(2) = 0 then
begin
x := 4; // one set of Variables per outcome
y := 4;
Really := true;
Result := 3.11; // notice that the Result is still the returning Variable outside of the ().
end else
begin
x := 20;
y := 30;
Really := False;
Result := 4.09;
end;
end;
Procedure Mainloop; // this is so you can test in Scar/Simba
var
a, b : integer;
Bool : boolean;
begin // Now we call the Function and assign Variable to our returning Data.
Writeln(Floattostr(didithappenwherewhen(a, b, bool)) + ' Our Extended.'); // It Export an Extended
Writeln(Inttostr(a) + ' Our First Integer or mouse coordinate X.'); //It Exports an Integer
Writeln(Inttostr(b) + ' Our Second Integer or mouse coordinate Y.'); //It Exports another Integer
Writeln(BoolToStr(bool) + ' Our Boolean.'); // and finally it Exports our Boolean;
end;
begin
Mainloop;
end.
Notice that the Variable's I assigned were the Same as the data they were export/returning.
Demonstration
Decided to add something that I used this Information to do. Just to show you the practical uses that are out there. I used Constants as I know that this won't be changing inside the function itself. This also lets others know that they won't get anything data returned from those parameters.
Simba Code:
function WithdrawItemTPA(const Colors : TIntegerArray; const Uptext : TStringArray; const Ammount : integer) : boolean;
var
x, y, i, c, d :integer;
WTPA, ITPA :TPointArray;
IATPA :T2DPointArray;
begin
result := False;
if not(BankScreen) or not(LoggedIn) then
exit;
C := (GetTimeRunning + 20000)
while (GetTimeRunning < c) do
begin
D:= High(Colors);
for i := 0 to D do
begin
if FindColorsTolerance(ITPA, Colors[i], MBx1, MBy1, MBx2, MBy2, 5) then
WTPA := CombineTPA(WTPA, ITPA);
end;
begin
IATPA := SplitTPAEx(WTPA, 10, 10);
SortATPAFrom(IATPA,Point(MBx1, MBy1));
D := High(IATPA);
for i := 0 to D do
begin
MiddleTPAEx(IATPA[i], x, y);
MMouse(x, y, 5, 5);
if WaitUpTextMulti(Uptext, 500) then
begin
ClickMouse2(False);
Break;
end;
end;
end;
If WaitOption('w-'+inttostr(Ammount), RandomRange(600, 1000)) then
begin
Result := True;
exit;
end;
if WaitOption('w-X', RandomRange(300, 600)) then
begin
Wait(RandomRange(600, 900));
TypeSend(inttostr(Ammount));
Result := true;
exit;
end;
end;
begin
Debug('Could Not Find');
result := false;
CloseBank;
NextPlayer(false);
exit;
end;
end;
Well that's all I have for now. Thanks for reading My Tutorial. Hopefully someone can actually learn from my first tutorial. If you like it Rep+
Were you able to learn anything from this? Let me know post a reply.
Any Feedback Appreciated