what is Recursive programming

Results 1 to 4 of 4

Thread: what is Recursive programming

  1. #1
    Recursive Guest

    Default what is Recursive programming

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

  2. #2
    Elliot Guest

    Default 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 ree-KUHR-zhion) 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 go-through). 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 made-up computer source language): <BR><BR><BR>CODELINE1 N=0; <BR>CODELINE2 IF N=&#060;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=&#060;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>

  3. #3
    Join Date
    Dec 1969

    Default Oh, curses! Oh, curses!

    See...cursed once and then re-cursed...<BR><BR>Sorry, couldn&#039t 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>&#060;%<BR>num = 17 &#039 or any value you want<BR><BR>factorial = 1<BR>For i = 2 To num<BR> &nbsp; factorial = factorial * i<BR>Next<BR>Response.Write num & "! is " & factorial & "&#060;P&#062;" <BR>%&#062;<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>&#060;%<BR>Function factorial( ByVal n )<BR> &nbsp; Dim temp<BR> &nbsp; If n &#062; 1 Then<BR> &nbsp; &nbsp; &nbsp; temp = n * factorial( n-1 )<BR> &nbsp; &nbsp; &nbsp; factorial = temp<BR> &nbsp; Else<BR> &nbsp; &nbsp; &nbsp; factorial = 1<BR> &nbsp; End If<BR>End Function <BR><BR>num = 17<BR><BR>Response.Write num & "! is " & factorial(num) & "&#060;P&#062;" <BR>%&#062;<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&#039s show the code path for factorial(4):<BR><BR>factorial(4):<BR> &nbsp; if 4 &#062; 1? Yes, so the result of this call is 4 * factorial(3).<BR>factorial(3):<BR> &nbsp; if 3 &#062; 1? Yes, so the result of this call is 3 * factorial(2).<BR>factorial(2):<BR> &nbsp; if 2 &#062; 1? Yes, so the result of this call is 2 * factorial(1).<BR>factorial(1):<BR> &nbsp; if 1 &#062; 1? No, so the result of this call is just 1.<BR>BACK to factorial(2):<BR> &nbsp; 2 * factorial(1) is thus 2 * 1 which is 2<BR>BACK to factorial(3):<BR> &nbsp; 3 * factorial(2) is thus 3 * ( 2 * 1 ) which is 6<BR>BACK to factorial(4):<BR> &nbsp; 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&#039t, 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>

  4. #4
    Join Date
    Dec 1969

    Default Hmmm...not sure I agree...

    I am surprised to see the GOTO version called "recursion." I&#039m not sure that it technically meets the definition, since it is just an old-fashioned variant on a DO WHILE loop.<BR><BR>I&#039d 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