Rich
06-29-2009, 02:13 AM
Welcome to my tutorial:
Procedures & Functions
This is where the beginners should begin. In this tutorial, you are going to learn how to write and use procedures and functions, and what the differences are.
Procedures are probably used more than functions, but that it not to say that functions aren't important. A procedure is a section of code which can be called upon to be used.
As an example, I'm going to show how you would find a colour, using both procedures and functions. To start off, procedures...
program ProsAndFuncsTut;
var
FoundColour : Boolean;
X, Y : Integer;
procedure FindColour;
begin
if FindColor(X, Y, 255, 0, 0, 500, 500) then
FoundColour:= True
else
FoundColour:= False;
end;
{The FindColour procedure above can be shortened to
procedure FindColour;
begin
FoundColour:= FindColor(X, Y, 255, 0, 0, 500, 500);
end;
I wrote it in the long way to show you exactly what it does}
procedure ClickColour;
begin
if (FoundColour = True) then
ClickMouse(X, Y, True);
end;
{The above ClickColour procedure can be shortened to
procedure ClickColour;
begin
if FoundColour then
ClickMouse(X, Y, True);
end;
But I wrote it like that to show exactly what it does.}
begin
FindColour;
ClickColour;
end.
When using a procedure to do what I told the script to do, you can see that I had to create a variable:
var
FoundColour : Boolean; //This one!
X, Y : Integer;
If I was to do this using a function, it would look like this:
program ProsAndFuncsTut;
var
X, Y : Integer;
function FindColour : Boolean;
begin
if FindColor(X, Y, 255, 0, 0, 500, 500) then
Result:= True
else
Result:= False;
end;
{The FindColour function above can be shortened to
function FindColour : Boolean;
begin
Result:= FindColor(X, Y, 255, 0, 0, 500, 500);
end;
I wrote it in the long way to show you exactly what it does}
procedure ClickColour;
begin
if (FindColour = True) then
ClickMouse(X, Y, True);
end;
{The above ClickColour procedure can be shortened to
procedure ClickColour;
begin
if FindColour then
ClickMouse(X, Y, True);
end;
But I wrote it like that to show exactly what it does.}
begin
ClickColour;
end.
{You'll notice I didn't call FindColour in my mainloop.
This is because I call it in ClickColour}
As you can see, there is no more variable called "FoundColour", instead, the function turns into a variable. Just like a normal variable, I have to declare which type it is. I do this after the name of my function as you can see in the example above. Also, in the function, you do not write "FunctionName:= True", you write "Result:= True".
var
FoundColour : Boolean;
//////////////////\\\\\\\\\\\\\\\\\\
function FindColour : Boolean;
Functions can be more than just booleans though. Examples:
program ProcsAndFuncsTut;
function IntegerFunc : Integer;
begin
Result:= 1 + Random(999);
end;
begin
IntegerFunc;
WriteLn(IntToStr(IntegerFunc));
end.
program ProcsAndFuncsTut;
function StringFunc : String;
begin
Result:= 'Hi everyone!';
end;
begin
StringFunc;
WriteLn(StringFunc);
end.
The last end in a procedure or function should have a semi-colon ( ; ) instead of a period ( . ).
Just remember, for every begin there must be an end, for every repeat an until, and for every if a then.
Richard.
Procedures & Functions
This is where the beginners should begin. In this tutorial, you are going to learn how to write and use procedures and functions, and what the differences are.
Procedures are probably used more than functions, but that it not to say that functions aren't important. A procedure is a section of code which can be called upon to be used.
As an example, I'm going to show how you would find a colour, using both procedures and functions. To start off, procedures...
program ProsAndFuncsTut;
var
FoundColour : Boolean;
X, Y : Integer;
procedure FindColour;
begin
if FindColor(X, Y, 255, 0, 0, 500, 500) then
FoundColour:= True
else
FoundColour:= False;
end;
{The FindColour procedure above can be shortened to
procedure FindColour;
begin
FoundColour:= FindColor(X, Y, 255, 0, 0, 500, 500);
end;
I wrote it in the long way to show you exactly what it does}
procedure ClickColour;
begin
if (FoundColour = True) then
ClickMouse(X, Y, True);
end;
{The above ClickColour procedure can be shortened to
procedure ClickColour;
begin
if FoundColour then
ClickMouse(X, Y, True);
end;
But I wrote it like that to show exactly what it does.}
begin
FindColour;
ClickColour;
end.
When using a procedure to do what I told the script to do, you can see that I had to create a variable:
var
FoundColour : Boolean; //This one!
X, Y : Integer;
If I was to do this using a function, it would look like this:
program ProsAndFuncsTut;
var
X, Y : Integer;
function FindColour : Boolean;
begin
if FindColor(X, Y, 255, 0, 0, 500, 500) then
Result:= True
else
Result:= False;
end;
{The FindColour function above can be shortened to
function FindColour : Boolean;
begin
Result:= FindColor(X, Y, 255, 0, 0, 500, 500);
end;
I wrote it in the long way to show you exactly what it does}
procedure ClickColour;
begin
if (FindColour = True) then
ClickMouse(X, Y, True);
end;
{The above ClickColour procedure can be shortened to
procedure ClickColour;
begin
if FindColour then
ClickMouse(X, Y, True);
end;
But I wrote it like that to show exactly what it does.}
begin
ClickColour;
end.
{You'll notice I didn't call FindColour in my mainloop.
This is because I call it in ClickColour}
As you can see, there is no more variable called "FoundColour", instead, the function turns into a variable. Just like a normal variable, I have to declare which type it is. I do this after the name of my function as you can see in the example above. Also, in the function, you do not write "FunctionName:= True", you write "Result:= True".
var
FoundColour : Boolean;
//////////////////\\\\\\\\\\\\\\\\\\
function FindColour : Boolean;
Functions can be more than just booleans though. Examples:
program ProcsAndFuncsTut;
function IntegerFunc : Integer;
begin
Result:= 1 + Random(999);
end;
begin
IntegerFunc;
WriteLn(IntToStr(IntegerFunc));
end.
program ProcsAndFuncsTut;
function StringFunc : String;
begin
Result:= 'Hi everyone!';
end;
begin
StringFunc;
WriteLn(StringFunc);
end.
The last end in a procedure or function should have a semi-colon ( ; ) instead of a period ( . ).
Just remember, for every begin there must be an end, for every repeat an until, and for every if a then.
Richard.