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. :)
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. :)