Posts: 3,616
Threads: 287
Joined: Jan 2003
Hmm, I've seen a lot about something called recursion lately.
Problem is, I don't know what it is...Something to do with loops?
Can anyone tell me?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 2,020
Threads: 24
Joined: Jun 2002
sub recursivesub (x as integer)
x = x + 1
print x
recursivesub(x)
end sub
this is recursive. calling a routine within itself to make a more complicated routine. but since qb's stack handling sucks, it's not a very fast or crash-proof method in qb.
but might i suggest checking out the power of: GOOGLE!
http://www.google.com/search?hl=en&ie=UT...=recursion
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 530
Threads: 17
Joined: May 2002
Quote:calling a routine within itself to make a more complicated routine
I always thought it was easier to write a recursive routine(sometimes) than a non recursive rotuine, and thats why its used, not to make it more complicated....
Posts: 3,368
Threads: 195
Joined: Jan 2003
the code becomes less complicated if you rank it -by size- but the actual workings of the code becomes more complicated.
Besides, recursion (at least in QB) uses dynamic arrays within arrays 9the QB stack size). Dynamic arrays are slow. Also, recursion is usually less efficient than iteration because you have to store the current line position of each instance and that eats a lot of stack space, too.
Of course it makes things easier on the outside but I like iterative more. (which is harder to program, usually)
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,616
Threads: 287
Joined: Jan 2003
Wouldn't that SUB of toonski84's create an infinate loop?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 6,419
Threads: 74
Joined: Mar 2002
Quote:Wouldn't that SUB of toonski84's create an infinate loop?
Yeah. That's why the base case is introduced.
Think for example about the caluculus of a factorial number. For example, 4! is 4*3*2*1. You can now express it in this manner:
Code:
x! = (x-1)! * x if x>1,
x! = 1 if x = 1
The second line is the base case. As you see, the first line uses a recursive call, as x! is defined using a previous value (x-1)!. The second line doesn't make any recursive call, so when x reaches 1 there won't be more calls. This, in QB...
Code:
FUNCTION factorial! (n!)
IF n!>1 THEN
result! = factorial!(n!-1)*n!
ELSE
result!=1
ENDIF
factorial! = result!
END FUNCTION
Posts: 3,616
Threads: 287
Joined: Jan 2003
My god!!! Calculus!!!
*screams and faints*
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Posts: 3,368
Threads: 195
Joined: Jan 2003
calculus? Wha--where? :roll:
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 3,288
Threads: 167
Joined: Nov 2001
Quote:My god!!! Calculus!!!
*screams and faints*
It ain't calculus. 1st year HS math would teach you factorial since they are needed for things like:
Combinations.
Posts: 2,020
Threads: 24
Joined: Jun 2002
i learned factorials in 6th grade prealgebra. if you were using the formula to estimate factorials and performing integrals and whatnot then yeah, it'd be calculus but he dont use it.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry