calling itself?
I read somewhere about a function/subrountine calling itself. i dunno how this works or if i misread it but can anyone explain how it works and what u would use it for ?

I believe what you are asking about is called recursion. A routine may do some work and call itself to do more work. There is an article on Using Recursion to Search a Directory in the knowledge base that might be useful for giving an example of the concept.

The classic example of this (recursion) is the calculation of factorial numbers.
The factorial numbers are defined such that the factorial of a natural number n is the product of all positive integers less than or equal to n. The fifth number in the series is thus 5 * 4 * 3 * 2 * 1. The 6th numnber is 6 * 5 * 4 * 3 * 2 * 1. And, so on. If you look at these two series though, you will see that the 6th number is equal to 6 * the 5th factorial. And, so on. So, you could easily write a factorial function that is recursive. For instance Code:
This function will recursively call itself. You just have to ensure that a recursive function has an "exit"  else it will recursively call itself until it ties up all avaialble memory, and then it will crash. (Note that the results of this function grow so quickly as N increases that it will overflow a long variable type when N = 12! As a result, you will probably want to use doubles, instead of longs. Also note that the factorial function is also one that is easy to write nonrecursively as well, using a loop within the function.) Recursion is often used when each task you are performing is essentially identical to that "above" it Dinural mentioned searching a directory. Each subdirectory can be called by the same routine. Which will call the dir routine for each subfolder... And, so on. Another example of a "natural" fit for recursion is in a binary search. In a binary search, you are searching for a match in an ordered list. You start by setting the min and max limits to search, picking a midpoint in the list that is halfway between the min and max, and check to see if the value at that midpoint is = to, less than, or greater than the one you're looking for. If it's equal, then you are done  you have a match. Huzzah! And exit. If the value at this position is less than your target value, then you must go higher  set the min value to the current midpoint, pick a new midpoint halfway between the new min and the existing max limits, and call the binary search routine with these new values. On the other hand, if the value at this position is greater than your target value, then the match, if it exists, is less than where your're at, so set the max limit to the current position, set the midpoint to halfway between the min and the max, and call the binary search routine again. If maxmin = 1 and you haven't found a target value, then it isn't in the list. Edit by loquin: 
One of the most common mistakes, when programming recursive routines, is to get the exit conditions wrong. You'll know when that happens as you'll get an "Out of Stack Space" error message.
(Been there, done that, have the Tshirt :D ) 
There is an entry in the VB help pages on "Creating recursive procedures". It doesn't add a lot to Loquin's comments though.

I believe that Loquin's example illustrates the factorial function, not a Fibonacci Series. It is a good example, though!
Edit by loquin: Thanks for pointing that out, Elder Knight. I fixed the post. :o 
As Elderknight already said: the example given by Loquin is for calculating the factorial. To calculate the Fibonacci series you have to do:
Code:

Wim,
I would use Long rather than Integer. With Integer, you will get an overflow error with an input greater than 23. Even Long will throw an error if n > 45. Massive integer methods would be needed to go further  there are a few threads kicking about in this forum on the subject. Besides, that method is really slow. This is not recursive, but it flies in comparison. Code:
The first few Fibonacci numbers 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 *Integer dies here* 46368 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 *Long dies here* 
Yes, I've read elsewhere (wish I knew where), that while Fibonacci and Factorial producing functions are "classic" examples used to demonstrate recursion, they are actually poor examples because cleaner nonrecursive methods can be used to produce those series, so aren't a strong argument for the use of recursion.
I don't know the particular article I read. In that one, they gave a simple example where recursion is obviously the best and simplest choice over an iterative solution. But, since I don't want to do a lot of searching to try and locate that particular article, I'll add yet another page that talks about recursion. 
Solving Towers Of Hanoi is another example where recursion really makes solving much simpler:
http://www.xtremevbtalk.com/showpost...4&postcount=12 
Towers of Hanoi. Hey that sounds familiar. Maybe that was the example from the other article. If not, it was something similar to that.

Recursive procedures can be fun for producing fractal images such as The Koch Snowflake or the Sierpinski Curve. I wrote loads of similar programs in Logo when I was at college, along with some really complex (not to mention pretty) recursive programs for producing images of fractal trees, ferns, coral, etc. :D
These days I write commercial mainframe software for the credit industry  which is just as exciting! :chuckle: 
Quote:
Becaues recursion requires that you push variables onto the stack & pop them off on return, they aren't efficient in these examples. For instance, a nonrecursive factorial function would look something like: Code:

All times are GMT 6. The time now is 09:31 AM. 
Powered by vBulletin® Version 3.8.9
Copyright ©2000  2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by
DragonByte SEO v2.0.15 (Lite) 
vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©20012011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.