
what is Recursive programming
What is Recursive programming ?<BR>Can you give simple example?<BR>thanks

RE: what is Recursive programming
From WhatIs.com  My Favorite IT Dictionary (second to 4GuysFromRolla, of course!)<BR>************************************** ****<BR>In computer programming, a recursion (noun, pronounced reeKUHRzhion) is programming that is recursive (adjective), and recursive has two related meanings: <BR>1) A recursive procedure or routine is one that has the ability to call itself. This usually means that it has the capability to save the condition it was in or the particular process it is serving when it calls itself (otherwise, any variable values that have been developed in executing the code are overlaid by the next iteration or gothrough). Typically, this is done by saving values in register or data area stack before calling itself or at the beginning of the sequence where it has just been reentered. <BR><BR>2) A recursive expression is a function, algorithm, or sequence of instructions (typically, an IF, THEN, ELSE sequence) that loop back to the beginning of itself until it detects that some condition has been satisified. Here is a simple example (using a madeup computer source language): <BR><BR><BR>CODELINE1 N=0; <BR>CODELINE2 IF N=<10 THEN DO WRITE LETTER; <BR>CODELINE3 ELSE GOTO CODELINE6;<BR>CODELINE4 N=N+1;<BR>CODELINE5 GOTO CODELINE2;<BR>CODELINE6 ...some other instruction<BR><BR>Here, the instructions labeled CODELINE2 through CODELINE5 are recursive until the condition of N having the value of 10. "IF N=<10" means "If N has a value less than 10." "N=N+1" means "Add 1 to the current value of N." <BR>In mathematics, recursion has similar but more complicated meanings than it does when used in programming. <BR>

Oh, curses! Oh, curses!
See...cursed once and then recursed...<BR><BR>Sorry, couldn't resist.<BR><BR>Sure...<BR><BR>The classic example is calculating the factorial of a number.<BR><BR>Now calculating a factorial in a loop is no big deal:<BR><BR><%<BR>num = 17 ' or any value you want<BR><BR>factorial = 1<BR>For i = 2 To num<BR> factorial = factorial * i<BR>Next<BR>Response.Write num & "! is " & factorial & "<P>" <BR>%><BR><BR>The notation 17! in math means "17 factorial" which means 17 times 16 times 15 times....times 2 times 1.<BR><BR>Hokay?<BR><BR>But to calculate a factorial via recursion in VBScript, we do this:<BR><BR><%<BR>Function factorial( ByVal n )<BR> Dim temp<BR> If n > 1 Then<BR> temp = n * factorial( n1 )<BR> factorial = temp<BR> Else<BR> factorial = 1<BR> End If<BR>End Function <BR><BR>num = 17<BR><BR>Response.Write num & "! is " & factorial(num) & "<P>" <BR>%><BR><BR>Do you see how that works? When you call factorial(17), the function looks to see if the argument (17 in this case) is greater than 1. If it is, then it calls ITSELF, passing one less than the argument (thus, factorial(16) in this case) and returns, as its value, the original argument times the result of calling itself.<BR><BR>So (to make an easier example than 17!), let's show the code path for factorial(4):<BR><BR>factorial(4):<BR> if 4 > 1? Yes, so the result of this call is 4 * factorial(3).<BR>factorial(3):<BR> if 3 > 1? Yes, so the result of this call is 3 * factorial(2).<BR>factorial(2):<BR> if 2 > 1? Yes, so the result of this call is 2 * factorial(1).<BR>factorial(1):<BR> if 1 > 1? No, so the result of this call is just 1.<BR>BACK to factorial(2):<BR> 2 * factorial(1) is thus 2 * 1 which is 2<BR>BACK to factorial(3):<BR> 3 * factorial(2) is thus 3 * ( 2 * 1 ) which is 6<BR>BACK to factorial(4):<BR> 4 * factorial(3) is thus 4 * ( 3 * ( 2 * 1 ) ) which is 24<BR><BR>And, indeed, 4! is 24.<BR><BR>So the secrets of recursion:<BR><BR>(1) You keep calling yourself again and again until you get to the end of some condition.<BR><BR>(2) You *better* have a test in there to check that you got to the end! If you don't, the calling goes on forever. (Well, not really. The computer will run out of memory soon enough and then barf on your feet.)<BR><BR><BR>

Hmmm...not sure I agree...
I am surprised to see the GOTO version called "recursion." I'm not sure that it technically meets the definition, since it is just an oldfashioned variant on a DO WHILE loop.<BR><BR>I'd want to see some real computer authority support that position.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

