SRL Archive - Research & Development Lounge

First function (simple :P)


Floor66

2008-02-23 04:10:13

Hello, his is my first(, or 2nd, idk) function:

function Count(Ending : Integer): Boolean;
var Starting : Integer;
begin
repeat
Starting := Starting + 1;
Writeln(''+IntToStr(Starting)+' msec');
until(Starting = Ending);
ClearDebug;
if (Rep = True) then
begin
Writeln('Counted to '+IntToStr(Ending * HowMuch)+' msec / ~'+IntToStr(Ending * HowMuch / 1000)+' sec[s]');
end else
begin
Writeln('Counted to '+IntToStr(Ending)+' msec / ~'+IntToStr(Ending / 1000)+' sec[s]');
end;
end;

And here's how it can be used:
program New;
var T : Integer;

const
Rep = True; //Repeat?
HowMuch = 5; //How much times to repeat?
Msecs = 1500; //How much millisecs to count? (1000 = 1sec)

function Count(Ending : Integer): Boolean;
var Starting : Integer;
begin
repeat
Starting := Starting + 1;
Writeln(''+IntToStr(Starting)+' msec');
until(Starting = Ending);
ClearDebug;
if (Rep = True) then
begin
Writeln('Counted to '+IntToStr(Ending * HowMuch)+' msec / ~'+IntToStr(Ending * HowMuch / 1000)+' sec[s]');
end else
begin
Writeln('Counted to '+IntToStr(Ending)+' msec / ~'+IntToStr(Ending / 1000)+' sec[s]');
end;
end;

begin
if (Rep = True) then
begin
repeat
T := T + 1;
Count(Msecs);
until(T = HowMuch);
end else
Count(Msecs);
end.

mixster

2008-02-23 05:54:34

I'm sorry, but that made no sense to me (reason wise). Firstly, why do you use msecs in the count method when you have no waiting method so it would give you a completely inaccurate time? Also, you could use a 'for starting:= 1 to Ending do' to shorten it a bit. Next, you should always make everything possible involved in the function/procedure, as having stuff like rep being defined as a const can easily mean that it gets left out. Lastly, this can all be pushed into about 4 lines without the writeln's, but why would you want it to count to 1500 5 times in the debug box?

I'm sorry, because I know this sounds very negative and it is, but it's because, for a function, it's too simple and could be easily coded better.

Floor66

2008-02-23 06:27:07

This wasnt made with a sense, but just a simple test, i also wonder why i'd use this!

KoKouKo

2008-02-28 00:03:07

fixed your standards (i think) and cleaned it up

function Count(Ending: Integer): Boolean;
var
Starting : Integer;
begin
repeat
Starting := Starting + 1;
Writeln(IntToStr(Starting)+' msec'); // changed this line
until(Starting = Ending);
ClearDebug;
if(Rep = True)then
Writeln('Counted to '+IntToStr(Ending * HowMuch)+' msec / ~'+IntToStr(Ending * HowMuch / 1000)+' sec[s]');
else Writeln('Counted to '+IntToStr(Ending)+' msec / ~'+IntToStr(Ending / 1000)+' sec[s]');
end;