SRL Archive - OSR Outdated Tutorials

Functions


Bebe

2007-02-17 14:20:10

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?



Tutorial:

Ok so lets start off by opening SCAR 2.03, you should see something that resembles this:

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

2007-02-17 14:30:19

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

2007-02-17 14:43:38

I consider anything besides "Successfully compiled" an error :p

Bobarkinator

2007-02-17 14:44:26

Good tutorial. Taught me a few things

bullzeye95

2007-02-17 14:47:24

From: Bebemycat2
I consider anything besides "Successfully compiled" an error :p


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

Bebe

2007-02-17 14:49:39

From: bullzeye95
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

2007-02-25 10:59:39

It does have that "NEWLINE" thing whenever you use SRL :p

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

rscheater13

2007-02-28 20:58:57

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

2007-02-28 21:53:21

From: Bebemycat2
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

2007-02-28 22:12:27

From: Jason2gs
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

2007-02-28 23:20:11

Bebe explains things so well. Yay for Megan! (8

Hey321

2007-03-04 07:32:45

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

2007-03-04 08:05:40

inventory.scar