SRL Archive - Outdated Tutorials

Procedure and Functions{How To Use/Differences Between Them}


Kik

2007-10-24 16:27:57

Procedures and Functions
by Kik

Welcome to my tutorial (in progress) on Procedures and Functions!

Yes, there are like 5 other tuts on this

*Under construction*

*assumes you know how the very basics of SCAR : EX: How to make it say 'hello' in the Debug Box*

Chapters…
1.0 ) What is a Procedure and a Function?
2.0 ) Procedures - How do I make one and how do they work?
3.0 ) Functions - How do I make one and how do they work?
4.0 ) Declaring Procedures and Functions with Variables (What?)




1.0 ) What is a Procedure and a Function?


What's a procedure? -
You can think of procedures as pre-defined sets of directions or instructions for SCAR to carry out. They make scripting much easier so that you don't have to type the same code over and over again if you want to use it multiple times.

What's a function? -
A function is extremely similar to a procedure, except a function returns something. Whoah! What do you mean by 'returns something'? Don't worry, I'll get to that in a little. First lets start off with procedures.


2.0 ) Procedures - How do I make one and how do they work?


It is very easy to add your own procedure to any script. Here's how.

First, lets start off with the basic SCAR setup.

program new;
begin
end.

Now, lets say we want to make a procedure that writes out 'Kik is teh awesome!' using WriteLn.

Writeln('Kik is teh awesome!');

So, lets get to the procedure. The basic setup for a procedure goes as follows.

procedure WriteStuff; //You can replace 'WriteStuff' with whatever name your procedure has.
begin
//The stuff you want the procedure to do.
end;

the 'begin' tells the procedure where the code will start. the 'end;', as you have probably guessed, tells the procedure where the code is ending.

So, now we simply add the code above with the code above it to make…

procedure WriteStuff;
begin
Writeln('Kik is teh awesome!');
end;

Tada! Your first procedure (or your tenth, if you've already been experimenting). Now, lets add that into the script. To do so, you simply place the procedure code within the script as follows…

program new;

procedure WriteStuff;
begin
Writeln('Kik is teh awesome!');
end;

begin
end.

But how do we let SCAR know when to perform the procedure WriteStuff? Well, that's easy! All you need to do is write the name of the procedure within the regular script to tell SCAR what to do, like this.

program new;

procedure WriteStuff;
begin
Writeln('Kik is teh awesome!');
end;

begin
WriteStuff; //this tells SCAR to perform WriteStuff.
end.

Now run that script. See what happens? Wasn't that fun! Now you can see why procedures are a neccesity to SCAR scripters.


3.0 ) Functions - How do I make one and how do they work?


Functions are slightly more complicated, but have no fear! Kik is here!

As I said before, functions act almost similarly to procedures, however, as I also said before, they return something. This simply means that the function is equal to something, say an integer or a string (examples below). So, lets say your function returns an integer. You could therefore make, say, variable X:= MyFunction. But how do we let the function know what to return? I'll cover that.

First, lets start off with the basic syntax for a function.

function FunctionNameHere: TypeOfVariableItReturns;
begin
//What you want the function to do goes here
result:= Something;
end;

Ahhh! What's all that do? I'll cover it here.

FunctionNameHere - The name of the function, just like a procedure.

WhatToReturn
- The type of value the function returns. EX: int, string, char, boolean, array of int, etc.

Most commonly used variables [that are returned with Functions]
- Boolean : probably one of the most commonly used variables to return, a boolean either returns true or false.

boolean1:= true;
boolean2:= false;

- String : this returns any text of any type. Strings are essentially an array of char, as described below.
string1:= 'hello everybody 67!';
char1:= string1[10];

result:= - The result is the value you want the function to return. So lets say you want the function to return 2 times 2. You could do result:= 2 * 2. I'll cover more as I go on.

So, lets get down and dirty (with functions, of course). Now that I've covered all the terms, lets get an example.

function MyName: string;
begin
result:= 'Kik';
end;

Now lets implement that into a script, much like we did with a procedure. However, this time, because the function MyName returns a string, we can't just simply declare it, because it doesn't know what do with it. So we have to add a WriteLn to it.

program new;

function MyName: string;
begin
result:= 'Kik';
end;

begin
Writeln(MyName);
end.

But why not just do Writeln('Kik')? Well, the example above is an extremely simple one. functions and procedures can hold many values, but I'm not going into that depth yet. Here's another example that shows the power of functions, especially with a boolean.

program new;
var I: integer;

function FindTheColor: boolean;
var mx, my, x, y: integer;
begin
GetMousePos(mx, my); //(mx, my) is the position of your mouse
//See my tutorial in my signature for more info on basic Color Finding.
if FindColor(x, y, 255, mx - 10, my - 10, mx + 10, my + 10) then
result:= true;
end;

begin
repeat
wait(300);
until(FindTheColor);
end.

In this script, I use the funciton FindTheColor. For those of you new to booleans, a boolean is a variable that is either true or false. The function gets the Mouse Position, (mx, my) and if it finds the color 255 (basic red) in a 20 by 20 box around the mouse then the function returns true. In the mainloop, it repeats wait(300) until FindTheColor [is true].

NOTE: When declaring variables inside a procedure (mx, my, x, y), the variables can only be used inside that procedure. To SCAR, mx, my, x, and y don't exist outside this procedure.

4.0 ) Declaring Variables within Procedures and Functions (What?)

Declaring variables within procedures and functions? What's that? Well, that's the best way I could think of to say how to use variables within a procedure. So, lets say you want to make a procedure that writes out something (because you don't want to just use plain WriteLn), but you want to write something different every time. How do we let SCAR know what to write? Heres an example.

program new;

procedure WriteSomething(Text: string);
begin
Writeln(Text);
end;

begin
WriteSomething('Kik is teh awesome!');
end.

As you can see, in the line that we declare the procedure we also declare the variable Text as a string. We then tell SCAR that this procedure should write out whatever Text happens to be. 'Var' is not needed when declaring a variable in a procedure as it is already assumed by SCAR.

Another one of my favorite ways of using a procedure is for Debugging (Having SCAR right out when commands are followed through IE: 'Tree Found', 'Dropped Inventory')

program new;
var DoIDebug: boolean;

procedure Debug(Write: string);
begin
if (DoIDebug = True) then
Writeln(Write);
end;

begin
DoIDebug:= True;
Debug('Hi all');
DoIDebug:= False;
Debug('Where did this text go?');
end.
As you can see, Debug will only write out text if DoIDebug is true.


Declaring a function with a variable is pretty much the same as in procedures. Here's another example for you that uses integers.


program new;

function Subtract(Num1: integer; Num2: integer): integer; //can also be said (Num1, Num2: integer)
begin
result:= Num1 - Num2;
end;

begin
Writeln(IntToStr(Subtract(5, 2)));
end.
When writing an integer, you must use IntToStr(..) or SCAR will give you an error.

Well, you've reached the end of my tutorial. I hope this one actually gets more than 3 posts like my Color Finding tut, but, hey, at least it helped you! If there's anything I missed, needs clarification, things to add, etc, just send me a PM.

HellBoyz

2007-10-27 00:17:53

Nice, although there are numbers of tut that talk about the simple of procedure and function but you have make it stand out of the cloud and the explanation was good and clear and some example of yours. Nice

drizzt

2007-10-28 13:17:56

very nice tut, helped me with functions a lot, Thanx m8

ShowerThoughts

2007-10-28 13:24:47

Gj ;)

Kik

2007-10-28 14:20:59

Thanks… I just did a search and found like 5 other tutorials on making procedures/functions… another tutorial can never hurt :p

thedarkmage

2007-10-28 20:51:18

pretty good guide, helps noobs alot

knives

2007-11-18 08:18:54

I just thought id say "wow" on both your tuts they really brought things back to basics and helped me figure both colour finding and functions out.

P.S. Your work has not gone in vain.

kingarabian

2007-11-19 18:36:15

Thx man appreciate your tut. Helped me understand functions =p.

-Spartan-

2007-11-20 22:57:39

Good tut. Clarified the difference between functions and procedures. Will be helpful in creating scripts. Thanks.

xinez

2007-11-30 02:34:34

Thanks, this solved a lot of my unanswered questions.

Jackrawl

2007-12-02 09:57:51

Good job, and happy birthday.

The Old lee

2007-12-07 05:29:22

Sweet! Helped me alot.

gerauchert

2007-12-07 08:04:30

very clear, good work!

i know that i didnt know how to use functions my first few months scripting because there wasnt a good tut on it

nicely done :D

exps35

2008-01-08 00:11:48

From: gerauchert
very clear, good work!

i know that i didnt know how to use functions my first few months scripting because there wasnt a good tut on it

nicely done :D


Ditto. I have yet to find a good tut on functions.
Until this, that is. It definately helped clear some things up that I had never learned with functions :)
Thanks alot.

WhoCares357

2008-01-08 07:08:25

From: exps35
Ditto. I have yet to find a good tut on functions.
Until this, that is. It definately helped clear some things up that I had never learned with functions :)
Thanks alot.


Create Your Own Commands

I should have put that one in the Intermediate Section, since it is a little bit more than just plain functions.

BTW, nice tutorial, Kik. You used adequate examples and explained it in a simplistic, but helpful way.

Prince

2008-02-06 02:07:11

OK a nooby quesiton but here it goes.

if (FindDTM(LobsterCage,Dx,Dy,MIX1,MIY1,MIX2,MIY2))then

in that line, do i gotta replace the mix1, etc stuff with the box of the inventory that i wish it to search? or leave it like that. thanks in advance

yours,
prince

Cazax

2008-02-06 12:12:21

From: prince
OK a nooby quesiton but here it goes.

if (FindDTM(LobsterCage,Dx,Dy,MIX1,MIY1,MIX2,MIY2))then

in that line, do i gotta replace the mix1, etc stuff with the box of the inventory that i wish it to search? or leave it like that. thanks in advance

yours,
prince


You can leave it at MIX1 becouse DTMs looks for the exact color, size, etc…