From: | Ralf Berkvens |
Date: | 8 Aug 2000 at 10:14:48 |
Subject: | Re: Investigator |
> Recursion is simple, but has a few pitfalls. All it is is that you have a
> function which calls itself. The one thing you MUST make sure of though is that
> anything specific to the current iteration is a local variable.
>
Obviously you must also make sure that there is a valid ending condition,
like a loop-counter (which counts how deep you are (how many recusrsions there
have been), and stops if a certain level is reached). The loop-counter is useful
if the programmer include file B from file A and file A from file B, and decides
to see if your program can cope with this (and yes, I have been known to do
these things oout of boredom :).
The ending condition should be placed as the first line of your function (at
*function header*
if (end-condition is met) then function return something;
*rest of function, including the recursive call to this one*
*end function*
What you return on the end-condition thingy is quite important. If you are
counting lines, for example the function could look like this (but don't copy
it, because it is bound to have mistakes in it :)
function count_lines {i, filename}
linecount=0 ; guess this one's local
if i>7 then function return 0 ; if there are too many files included (7 here)
if ReadFile(i, filename) ; try to read the file
while (not end of file)
end function ; end of function
As you see it's pretty simple (and even simpler when you remember blitz syntax
countlines function will return all the lines of all the files all added to-
gether, so it would look like so:
nprint(count_lines(0,firstfilename)) ; show amount of lines in this file and
; all files included from it.
Tres simple, no?
Ralf
---------------------------------------------------------------------
To unsubscribe, e-mail: blitz-list-unsubscribe@netsoc.ucd.ie
For additional commands, e-mail: blitz-list-help@netsoc.ucd.ie