Distort
06-06-2007, 06:30 PM
Basic functions : How to implement and create
Thanks to:
mastaraymond for cleaning up some of the code and offering a better way for "for" loops ;)
Although this tutorial is called basic, it is not for the utter noobie, thus why i posted it into the intermediates section.
In this tutorial we will be breaking down the steps of the script below (written by myself). I will explain what will make a function supior to a procedure for doing anything which will effect variables later.
program New;
var
output: extended;
input : string;
function sqr(mid:extended):extended;
var
i : integer;
other, moreother: extended;
begin
other:=mid/2.5;
for i:=0 to 10000 do
begin
moreother:=mid/other;
other:=moreother+other;
other:=other/2;
inc(i);
end;
result:=other;
end;
begin
input:=readln('What number do you want to sqr root?');
output:=sqr(strtofloat(input));
writeln('The square root of '+input+' is: '+floattostr(output));
end.
Ill assume you know how the layout of a script works.
So first, lets look at the main loop
begin
input:=readln('What number do you want to sqr root?');
output:=sqr(strtofloat(input));
writeln('The square root of '+input+' is: '+floattostr(output));
end.
ok, what this is doing is first asking what you want to sqr root.
It will then make the output equal to the sqr of the string "input".
It will then writedown the line.
The only complicated part in this is this line:
output:=sqr(strtofloat(input));
ok, we have the float variable output and we have the string value of input. We need to convert the string to a float else the script will fuck up. That explains the strtofloat(input) (and what do you know, it is also a function) part, now what about the sqr(...) part?
Well, earlier on in the script we defined a function called sqr using this
function sqr(mid:extended):extended;
What this does is create a new function called sqr, it then defines what the inputs are (in this case, "mid" which is a extended number) we then define what attribute the function is, and in this case its also a extended number. If we put
function sqr(mid:extended):integer;
instead, it would NOT make it capatable with the float "output", plus the answer will be rounded (not what we want realy).
after declaring the function, we can start on telling it what to do
First we choose some local variables for it. We use local variables so as not to not get confused about what is doing what. If we had a global var called X, and a local var also called X, it would automatically choose the local one.
var i : integer;
var other, moreother: extended;
after that we do some calculations
begin
other:=mid/2.5;
for i:=0 to 10000 do
begin
moreother:=mid/other;
other:=moreother+other;
other:=other/2;
inc(i);
end;
what this calculation will do is find out the square root of something, (within 1/100000th of a decimal point. I chose this because it is more accurate than what you will need, plus you can only writeln a certain amount of decimal points.)
So, now we have our answer, but we need to put this answer into the sqr variable (yes, the sqr is now a variable with special powers. If you had a:=b you know that the variable a == the variable b, and that b could be a function) which is what this line does:
result:=other;
Scar will reserve the "result" as the actual value of the function name. It equals to other because other is a local variable (only accessable to that function) and that is the last part of our calculation.
So, with our new gained knowledge in functions, lets make a funtion which will add a + b.
First, we declare the function
function AddMe:integer;
Now we need to know whats being added, so we add this:
function AddMe(Add, Me:integer):integer;
Then we begin to script the code:
function AddMe(Add, Me:integer):integer;
begin
result:=Add+Me;
end;
How would we run this into a program?
program new;
var a, b, d:integer;
var c:string;
function AddMe(Add, Me:integer):integer;
begin
result:=Add+Me;
end;
begin
c:=readln('What do you want to start with?');
a:=strtoint(c);
c:=readln('What do you want to add to it?');
b:=strtoint(c);
d:=AddMe(a, b);
writeln('The answer to '+inttostr(a)+'+'+inttostr(b)+'='+inttostr(d));
end.
Broken down, it will ask for a, ask for b, make d equal to AddMe which is equal to a+b, then output it all.
Now, why are functions superior to procedures at points?
Well, the answer is simple realy. When you want a procedure to add something together, you have to make it edit a global variable. This is fine, but it means that soon you will have alot of global variables running amoock in your RAM.
With a function it contains everything it needs to edit within itself. This means that if you want A:=AddMe, it will first make AddMe a variable and make the answer of itself (all that math we just did) into A.
If we were to use a procedure, we would have to make a global variable, make it directly influence that variable and that variable HAS to stay there until the script ends, whilst in a function it will let it self be overwritten whilst the script is running, untill it has been called upon again, and by then it will just move its position in the RAM somewhere else, a global variable will not do this.
The global variable will stick into memory, and if you have enough stuff going on, your memory will run out and your computer may churn to a stop.
However, if you dont want to calculate anything, a procedure is better because you dont have to make it equal anything, as long as it has its inputs, it will click that button happily.
Thank you for reading, i hope this helped.
:spot:
If you need some proof about scar taking a local variable over a global one, look at this following script. It does a basic procedure with the local variable i, influenced by the variable bob. Since we declare the global i as 1, and b as i+1, the aput should be 4 because 2+2=4 and 1+1=2 (since we are adding the local i to the bob, which is b, which is 2).
Sorry if that sounds complicated, it's just how I worded it.
program new;
var
i,b:integer;
procedure test(bob:integer);
var
i:integer;
begin
i:=bob;
i:=i+bob;
writeln(inttostr(i));
end;
begin
i:=1;
b:=i+1;
test(b);
end.
Thanks to:
mastaraymond for cleaning up some of the code and offering a better way for "for" loops ;)
Although this tutorial is called basic, it is not for the utter noobie, thus why i posted it into the intermediates section.
In this tutorial we will be breaking down the steps of the script below (written by myself). I will explain what will make a function supior to a procedure for doing anything which will effect variables later.
program New;
var
output: extended;
input : string;
function sqr(mid:extended):extended;
var
i : integer;
other, moreother: extended;
begin
other:=mid/2.5;
for i:=0 to 10000 do
begin
moreother:=mid/other;
other:=moreother+other;
other:=other/2;
inc(i);
end;
result:=other;
end;
begin
input:=readln('What number do you want to sqr root?');
output:=sqr(strtofloat(input));
writeln('The square root of '+input+' is: '+floattostr(output));
end.
Ill assume you know how the layout of a script works.
So first, lets look at the main loop
begin
input:=readln('What number do you want to sqr root?');
output:=sqr(strtofloat(input));
writeln('The square root of '+input+' is: '+floattostr(output));
end.
ok, what this is doing is first asking what you want to sqr root.
It will then make the output equal to the sqr of the string "input".
It will then writedown the line.
The only complicated part in this is this line:
output:=sqr(strtofloat(input));
ok, we have the float variable output and we have the string value of input. We need to convert the string to a float else the script will fuck up. That explains the strtofloat(input) (and what do you know, it is also a function) part, now what about the sqr(...) part?
Well, earlier on in the script we defined a function called sqr using this
function sqr(mid:extended):extended;
What this does is create a new function called sqr, it then defines what the inputs are (in this case, "mid" which is a extended number) we then define what attribute the function is, and in this case its also a extended number. If we put
function sqr(mid:extended):integer;
instead, it would NOT make it capatable with the float "output", plus the answer will be rounded (not what we want realy).
after declaring the function, we can start on telling it what to do
First we choose some local variables for it. We use local variables so as not to not get confused about what is doing what. If we had a global var called X, and a local var also called X, it would automatically choose the local one.
var i : integer;
var other, moreother: extended;
after that we do some calculations
begin
other:=mid/2.5;
for i:=0 to 10000 do
begin
moreother:=mid/other;
other:=moreother+other;
other:=other/2;
inc(i);
end;
what this calculation will do is find out the square root of something, (within 1/100000th of a decimal point. I chose this because it is more accurate than what you will need, plus you can only writeln a certain amount of decimal points.)
So, now we have our answer, but we need to put this answer into the sqr variable (yes, the sqr is now a variable with special powers. If you had a:=b you know that the variable a == the variable b, and that b could be a function) which is what this line does:
result:=other;
Scar will reserve the "result" as the actual value of the function name. It equals to other because other is a local variable (only accessable to that function) and that is the last part of our calculation.
So, with our new gained knowledge in functions, lets make a funtion which will add a + b.
First, we declare the function
function AddMe:integer;
Now we need to know whats being added, so we add this:
function AddMe(Add, Me:integer):integer;
Then we begin to script the code:
function AddMe(Add, Me:integer):integer;
begin
result:=Add+Me;
end;
How would we run this into a program?
program new;
var a, b, d:integer;
var c:string;
function AddMe(Add, Me:integer):integer;
begin
result:=Add+Me;
end;
begin
c:=readln('What do you want to start with?');
a:=strtoint(c);
c:=readln('What do you want to add to it?');
b:=strtoint(c);
d:=AddMe(a, b);
writeln('The answer to '+inttostr(a)+'+'+inttostr(b)+'='+inttostr(d));
end.
Broken down, it will ask for a, ask for b, make d equal to AddMe which is equal to a+b, then output it all.
Now, why are functions superior to procedures at points?
Well, the answer is simple realy. When you want a procedure to add something together, you have to make it edit a global variable. This is fine, but it means that soon you will have alot of global variables running amoock in your RAM.
With a function it contains everything it needs to edit within itself. This means that if you want A:=AddMe, it will first make AddMe a variable and make the answer of itself (all that math we just did) into A.
If we were to use a procedure, we would have to make a global variable, make it directly influence that variable and that variable HAS to stay there until the script ends, whilst in a function it will let it self be overwritten whilst the script is running, untill it has been called upon again, and by then it will just move its position in the RAM somewhere else, a global variable will not do this.
The global variable will stick into memory, and if you have enough stuff going on, your memory will run out and your computer may churn to a stop.
However, if you dont want to calculate anything, a procedure is better because you dont have to make it equal anything, as long as it has its inputs, it will click that button happily.
Thank you for reading, i hope this helped.
:spot:
If you need some proof about scar taking a local variable over a global one, look at this following script. It does a basic procedure with the local variable i, influenced by the variable bob. Since we declare the global i as 1, and b as i+1, the aput should be 4 because 2+2=4 and 1+1=2 (since we are adding the local i to the bob, which is b, which is 2).
Sorry if that sounds complicated, it's just how I worded it.
program new;
var
i,b:integer;
procedure test(bob:integer);
var
i:integer;
begin
i:=bob;
i:=i+bob;
writeln(inttostr(i));
end;
begin
i:=1;
b:=i+1;
test(b);
end.