SRL Archive - Outdated Tutorials

Understanding Procedures and Functions!


Runaway

2009-09-25 07:41:40

Understanding:


Functions are used in scripting to make your life easier. If you need to use the same 20-line loop over and over in your script, functions and procedures are just what you need to save yourself time and effort. Let's get started.


1. What are Procedures and Functions?

1.1 Definitions
Procedure: a set of instructions for a computer that has a name by which it can be called into action; a series of steps followed in a regular definite order.
In short, a procedure is used to tell a script to execute other procedures or functions in a certain order. This can be done as many times as you please and thus comes in handy.


Function: a computer routine that performs a calculation with variables provided by a program and supplies the program with a single result.
A function is a procedure, but it has a result. These are used to not only execute a group of procedures, but also get a response from that grouping. Don't worry, this will be explained more in-depth later.


1.2 Components
A procedure always consists 4 components: Name, Start, Body, and an End.

Procedure Random; //Name
Begin //Start
Wait(500); //Body
Writeln('yo');
End; //End

A function always consists of 5 components: Name, Start, Body, Result, and an End.

Procedure Random: Boolean; //Name
Begin //Start
Wait(500); //Body
Result := Writeln('yo'); //Result
End; //End


2. A Low Down on Procedures and Functions

2.1 Basics
The main components of a procedure and function:

Name - Used to name your procedure or function, and this name will be put into a script when it is being called upon.

Procedure Whoa;
Begin
Wait(100);
End;
// Now whenever you need to wait for 100ms, you can just call the procedure "Whoa"
Begin
Whoa;
Writeln('Yo');
Whoa;

Constants - These must be set if you are using a value that will not change throughout the procedure and is going to be called upon more than once.

const
treecol = 1171156;
treecol2 = 19985737;
//instead of re-typing those colors out, you can use treecol and treecol2
//wherever they are needed in the script. Not only that, but if you need to change
//those colors, you can change them there and they will work for the rest of the script!

Variables - These must be set if you are using any variable in your procedure such as x or y for a mouse function.

Function FindLogs: Boolean;
Var
x, y: Integer; //These are used to store data in x and y. since the FindDTM procedure
//is using x and y, you need to let the function know that you will be storing data in them.
Begin
if not (LoggedIn) then Exit;
if FindDTM(x, y, LogDTM, MIX1, MIY1, MIX2, MIY2) then
begin
//blah blah blah
end;
end;

Start - You must always have a Begin at the beginning of any script, procedure, or function.

Procedure Hi;
Begin //you must ALWAYS have a begin. no matter what.
Writeln('hi');
End;

Body - The main body of your procedure or function, this is why you create procedures. This section holds the lines of script that you don't want to keep typing over and over again.

procedure CleanHerbs(herbscolor: Integer);
var
x, y, i: Integer;
begin //everything commented out is the "body" of the procedure.
// if (not(LoggedIn)) then Exit;
//if (not(FindColor(x, y, herbscolor, MIX1, MIY1, MIX2, MIY2))) then Exit;
//for i := 1 to InvCount do
//MouseItem(i, true);
//Inc(HerbsCleaned);
//wait(50+random(25));
//Disguise('Cleaned: '+IntToStr(HerbsCleaned)+' Herb(s)!');
end;

Result - Only in a function. This is a specific output that you want your procedure to give you, such as a true/false answer, or a string. Every function must have a result.

function Antiban: boolean;
var
x, y: Integer;
Begin
if not (LoggedIn) then Exit;
result := true; //results are used for failsafes generally. if you get a false result
//or a not(true) result, you know something went wrong and you have to try something else.
AntiRandoms;
//case random(220) of
//......
End;

End - All scripts, procedures, and functions need an end. Otherwise, the procedure will not end when you want it to.

Procedure Execute;
Begin
Writeln('kbai');
End; //EVERY begin needs and end. No matter what.

2.2 Parameters
Some procedures and functions can use a set of parameters that a user of the script can fill in. Parameters can be helpful when you are dealing with a situation which could use multiple inputs and still output the way you want it to.

procedure TimeStatus(WaitTime: Integer); //parameters are used to control the input.
//without the parameters for this procedure, there would have to be a set waittime,
//and people would have to change it manually every time they used it. This way,
//anyone who needs to change it can change it much more easily.
var
Hours, Minutes, Seconds, G: Integer;
Ticker: String;
begin
MarkTime(G);
while (WaitTime > TimeFromMark(G)) do
begin
Wait(500);
ConvertTime(WaitTime - TimeFromMark(G), Hours, Minutes, Seconds);
//...
end;
end;

2.3 Results

A result is required for all functions. This outputs the data you specify the function to output.

function BankHerbs: Boolean;
begin
if (not(LoggedIn)) then Exit;
result := OpenBankFast('vwb'); //EVERY function has a result. It can be true, false,
//or even a different function that will return as true or false. the possibilities are endless!
//...
end;


3. How are Procedures and Functions Used?


3.1 Repeating

Procedures and functions are used to help reduce the amount of lines that a script contains. If there is going to be lines and lines of redundant coding in your script, you should put those lines of code into a procedure or function. Doing this, you can call upon that code whenever you need to in the script.

3.2 Calling
To call your procedure or function in a script, you simply use the name you gave it when you created the procedure or function, and use it like any other SRL procedure.

procedure DeclarePlayers;
begin
HowManyPlayers:= 1;
NumberOfPlayers(HowManyPlayers);
CurrentPlayer:= 0;

Players[0].Name := '';
Players[0].Pass := '';
Players[0].Nick := '';
Players[0].Active := True;
end;

Begin
SetupSRL;
DeclarePlayers; //you call a procedure simply by using it's name and placing a semi-colon afterward.
//...
End.

END.
I hope you learned much about how procedures and functions work and the differences between the two!

-Runaway

Rich

2009-09-25 13:12:46

Procedure Random: Boolean;  //Name
Begin //Start
Wait(500); //Body
Writeln('yo'); //Result
End; //End
–>
function Random: Boolean;  //Name
Begin //Start
Wait(500); //Body
Writeln('yo'); //Result
End; //End

Also, that functions doesn't have a result. It's doing exactly the same as the procedure. I think you mean:
function Random : string;  //Name
Begin //Start
Wait(500); //Body
Result:=('yo'); //Result
End; //End

Very nice other than that.

Richard.

caused

2009-09-25 14:29:23

Haha, R1ch, I just wanted to point that out :).

Also you should Point out, that result is always a variable with the datatype the function it is returning, meaning you can use it as a normal var, and the last value it holds before the function exits is the return value.

For Examlpe (I hope that makes sense, also thats not the best explanation, it makes more sense, when youre having an tpointarray or seomthing xD… anyways…):

function HowManyTimesDidTheyPostSpam(username : string):integer;
begin
case username of
'harry' : result := 7000+Random(1000);
'R1ch' : result := 1000+Random(300);
'caused' : result := 400+Random(100);
end;

if result = 0 then
WriteLn('Invalid Username');

end;

Anyways good job! :)

Method

2009-09-25 17:28:12

Procedure Random: Boolean;  //Name
Begin //Start
Wait(500); //Body
Result := Writeln('yo'); //Result
End; //End

Since when does Writeln return a boolean? :confused:

caused

2009-09-25 17:29:27

From: Method
Procedure Random: Boolean;  //Name
Begin //Start
Wait(500); //Body
Result := Writeln('yo'); //Result
End; //End

Since when does Writeln return a boolean? :confused:


R1ch already pointed that problem out ;)

Method

2009-09-25 17:31:39

From: caused
R1ch already pointed that problem out ;)


And he edited the original text to what I posted (I think). From what I remember the original line was just:

Writeln('yo'); //Result

Rich

2009-09-25 17:31:51

Runaway, it should be a function, WriteLn does not return, and the return is a boolean, so it needs to be:

function Random : string;  //Name
Begin //Start
Wait(500); //Body
Result:=('yo'); //Result
End; //End

EDIT: Method, I posted before what is also in this post again.