Recursive Subs?

  • Thread starter Thread starter ExcelMonkey
  • Start date Start date
E

ExcelMonkey

Can someone explain to me what recursive subs do. Mu understanding i
that it is a sub that in effect calls itself. Why would you ever nee
this? What woudl be an example of this
 
Hi
similar questions was aske prior this day. Here's a repost:
Frank
------

one reason is the use for recursive algorithms. e.g. calculation of the
factorial (e.g. 5! =1*2*3*4*5):
Function fact(i as integer) as long
If i = 0 then
fact = 1
else
fact = i*fact(i-1)
end if
end function
Of course you can achieve this without recursion but this way its
'easier' to read.

you may have a look at the following site:
http://cse.unl.edu/~dsadofs/RecursionTutorial/index.php?s=algorithms
for more information (or just do a google search for 'recursive
algorithms')
 
They are generally used when you don't know in advance how
big your loop size is
and have problems defining an efficient loop structure.
For an example, see my posting today
re: delete folder Feb 4 2004 8:55AM

There are some problems for which recursion is the more
elegant apppoach (e.g. code readability)
but "Out of stack space" can be a problem if the stack
space is unduly constrained.

Kevin Beckham
 
Recursion is as when a procedure calls itself. It can be an extremely useful
technique, but has to be used with care.

The biog advantage is that it allows you to process when you have absolutely
no idea how many levels of your particular item will occur, and so if you
are processing a data type, and amongst that you find another set of that
datatype, you can just branch out of your current processing, process the
new item by calling itself, and then pick up where you were at the end. And
this can be done a number of times.

The best example I have is a VBScript that deletes files from the temp
folder. It builds an array of files to delete. It start in the temp
directory, and adds to the array. If one of the files is a directory, it
recursively calls itself for this new directory, and so on. Thus, I don't
have to do any testing on the number of sub-folders, and sub-folders of
sub-folders, etc.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Can someone explain to me what recursive subs do.

To define recursion, we must first define recursion...
 
;-)
That's an illustration of what can go wrong if the recursive
algorithm is poorly designed.

loop, infinite - see infinite loop
infinite loop - see loop, infinite
 
Back
Top