PDA

View Full Version : Functions



Bebe
02-17-2007, 07:20 PM
Functions By Bebemycat2

What is this going to cover?
This tutorial will explain a function and what their many uses are.

What will you need?

Some basic SCAR knowledge: http://www.villavu.com/forum/showthread.php?t=581?t=683



Tutorial:

Ok so lets start off by opening SCAR 2.03, you should see something that resembles this:
http://img70.imageshack.us/img70/6329/scar1vq1.jpg

If so than you're all set to learn :)

Functions:

What are they?
Functions are the same as procedures but they return a value.

What are the "values" it can return?
String - Text, string of characters
Integer - Whole numbers, positive and negative (i.e. 1, 3, -7)
Extended - Real numbers, positive and negative (i.e. 1.5, -2.6)
Boolean - True or False

And a few more, but these are the basic four variables used in most functions.

How do you make a function?
Well lets start of with a basic example. Say I wanted to convert American miles to metric kilometers.
To do this we need to find the conversion formula, in this case being miles * 1.609 = kilometers

So lets start out by making a function and naming it "MilesToKilometers" like so:
program New;

function MilesToKilometers


begin
end.

But now we have to add a variable so the use can input how many miles need to be converted, so we add this:
(miles:extended)

To get it to like like this:
program New;

function MilesToKilometers(miles:extended)


begin
end.

But wait that won't compile because we have to add the type of variable the function will return in the same line also. For this function you would want it to return an extended because when you are multiplying the miles by 1.609 you are not always going to get a "nice" number (whole number).

So we have to add ":extended;" to the end, like this:
program New;

function MilesToKilometers(miles:extended):extended;


begin
end.

Now lets add our "begin" and "end;", so we get it to look like this:
program New;

function MilesToKilometers(miles:extended):extended;
begin
end;


begin
end.

If you try to compile now then you will get a "Hint" like this:

Line 3: [Hint] (3:10): Variable 'Result' never used in script

Now you may be thinking to yourself "But I never defined a variable named Result", well SCAR did. Once you create a function, SCAR creates a local variable named Result.

What is Result? Well it is the result of your function that can be used by other areas in your script. It is also the part of that function that will be returned. Like in the case of our "MilestoKilometers" function, we are returning the amount of kilometers.

So lets get back to our script.

Sow now we have to add result in. So we type "Result:=" and then add our formula "miles * 1.609;"
"Miles" is out variable that was previously defined in the Function "title"
The "*" stands for multiplication.
1.609 is the amount of kilometers in each mile.

So you should have something like this:
program New;

function MilesToKilometers(miles:extended):extended;
begin
result:= miles * 1.609;
end;


begin
end.

Successfully compiled



You just created your first function!

To test my function I am going to use this script:
program New;

function MilesToKilometers(miles:extended):extended;
begin
result:= miles * 1.609;
end;

procedure WriteFunction;
begin
writeln('1 mile is equal to '+floattostr(MilesToKilometers(1.0))+'kilometers') ;
end;

begin
WriteFunction;
end.


1 mile is equal to 1.609kilometers

It works. :)

bullzeye95
02-17-2007, 07:30 PM
Good tutorial Bebe... for noobz :p

I did learn something though. I didn't know that 1 mile is equal to about 1.609 km.

Oh and where you put


If you try to compile now then you will get an error like this:


Line 3: [Hint] (3:10): Variable 'Result' never used in script

You should change your wording, because it's not really an "error".

Bebe
02-17-2007, 07:43 PM
I consider anything besides "Successfully compiled" an error :p

Bobarkinator
02-17-2007, 07:44 PM
Good tutorial. Taught me a few things

bullzeye95
02-17-2007, 07:47 PM
I consider anything besides "Successfully compiled" an error :p

It does say "Successfully compiled" ;). THEN, it says the hint.

Bebe
02-17-2007, 07:49 PM
It does say "Successfully compiled" ;). THEN, it says the hint.
But it isn't a good old clean "Successfully compiled" :)

Ok, lets stop arguing ;)

Edit: I fixed it, Happy! :p

Jason2gs
02-25-2007, 03:59 PM
It does have that "NEWLINE" thing whenever you use SRL :p

That's not technically a good old clean "Successfully compiled" :D

rscheater13
03-01-2007, 01:58 AM
Thanks a lot, it really helped, I knew nothing about functions except they were like procedures, but i have one question. I changed a procedure into a function, I needed the result, but now in the main loop it says there is an invalid number of parameters at the item that i changed into a function. Please help.

bullzeye95
03-01-2007, 02:53 AM
But it isn't a good old clean "Successfully compiled" :)

Ok, lets stop arguing ;)

Edit: I fixed it, Happy! :p

Happy!


Oh and can you post that function rscheater?

Ejjman
03-01-2007, 03:12 AM
It does have that "NEWLINE" thing whenever you use SRL :p

That's not technically a good old clean "Successfully compiled" :D

Easily fixable in scripts u want perfect...

newline := newline;

:P

WhiteShadow
03-01-2007, 04:20 AM
Bebe explains things so well. Yay for Megan! (8

Hey321
03-04-2007, 12:32 PM
Great tut, taught me functions XD. I have one question though, is there already a UseItem function out there? IF not i'm making one :).

Boreas
03-04-2007, 01:05 PM
inventory.scar