PDA

View Full Version : Procedures & Functions



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.

Coh3n
06-29-2009, 02:22 AM
Very nice, simple to understand. I wish you wrote this earlier, I didn't know the difference between a procedure and a function for the longest time. :p Had to figure that one out for myself. ;)

For this line:


if (FindColour = True) then


I don't think you need to say 'FindColor = True'. I'm pretty sure you can just say:


if FindColor then


If I'm wrong, well then :duh: and I apologize. :)

Anyway, good TUT, I'm sure this will help a lot of people.

Rep+ :)

Rich
06-29-2009, 02:26 AM
Thanks for the great comment and rep. About if (FindColour = True) then Yes, it could have been what you said, but I thought that the way I did it would be easier for beginners to understand, and that's who this tutorial is aimed at.

Richard.

Naum
06-29-2009, 02:29 AM
function FindColour : Boolean;
begin
if FindColor(X, Y, 255, 0, 0, 500, 500) then
Result:= True
else
Result:= False;
end;

to:

function FindColour : Boolean;
begin
Result := FindColor(X, Y, 255, 0, 0, 500, 500);
end;

Good tut, looks like your mirroring mine :) :p

Coh3n
06-29-2009, 02:34 AM
Thanks for the great comment and rep. About if (FindColour = True) then Yes, it could have been what you said, but I thought that the way I did it would be easier for beginners to understand, and that's who this tutorial is aimed at.

Richard.

I agree, but maybe add a little extra in there explaining why it doesn't HAVE to be there, that way they're learning even more from your TUT. :)

Rich
06-29-2009, 02:52 AM
function FindColour : Boolean;
begin
Result := FindColor(X, Y, 255, 0, 0, 500, 500);
end;

Added a bit about that.


Good tut, looks like your mirroring mine :) :p

O rly? :p


I agree, but maybe add a little extra in there explaining why it doesn't HAVE to be there, that way they're learning even more from your TUT. :)

Added.

Thanks guys!

Coh3n
06-29-2009, 02:53 AM
Thanks guys!

No problem, TBH I actually really like this TUT, I don't know why, but I do. :)

StickToTheScript
03-19-2012, 01:03 AM
Thanks man! This TUT helped me out a Ton! Thanks!