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.
Now, lets say we want to make a procedure that writes out 'Kik is teh awesome!' using WriteLn.
SCAR Code:
Writeln('Kik is teh awesome!');
So, lets get to the procedure. The basic setup for a procedure goes as follows.
SCAR Code:
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...
SCAR Code:
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...
SCAR Code:
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.
SCAR Code:
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.
SCAR Code:
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.
SCAR Code:
boolean1:= true;
boolean2:= false;
- String : this returns any text of any type. Strings are essentially an array of char, as described below.
SCAR Code:
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.
SCAR Code:
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.
SCAR Code:
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.
SCAR Code:
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.
SCAR Code:
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')
SCAR Code:
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.
SCAR Code:
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.