Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
recursive calls Posted by Nickname on 13 Mar 2003 at 2:33 PM
Hi;
Can someone tell me if on the net exists any site or tutorial that explains these recursive procedures or functions.
Thanks
Report
Re: recursive calls Posted by sweeney on 13 Mar 2003 at 3:44 PM
: Hi;
: Can someone tell me if on the net exists any site or tutorial that explains these recursive procedures or functions.
: Thanks
:
hi recursive??
Report
Re: recursive calls Posted by Phat Nat on 13 Mar 2003 at 4:36 PM
: Hi;
: Can someone tell me if on the net exists any site or tutorial that explains these recursive procedures or functions.
: Thanks

Recursive functions are pretty easy to understand, but very confusing to use. Basically, you make a procedure that calls itself. This is done to accomplish things that would otherwise take alot of coding.

An example of recursive procedure is a FILL procedure in graphics or an equation such as the "Towers of Hanoi" (complicated ancient Monk problem)

Here is a basic recursive function:

FUNCTION Recursion(X : Word) : Word;
Begin
     If X < 10 Then
        Recursion := Recursion(X+1);
     WriteLn('Recursion Function #',X);
End;

Begin
     Recursion(1);
End.


If you were to run this, you would have the following steps taken:
[01] Recursion(1) is called
[02] X(=01) < 10 Then Recursion := Recursion(X+1) is called;
[03] X(=02) < 10 Then Recursion := Recursion(X+1) is called;
[...]
[08] X(=07) < 10 Then Recursion := Recursion(X+1) is called;
[09] X(=08) < 10 Then Recursion := Recursion(X+1) is called;
[10] X(=09) < 10 Then Recursion := Recursion(X+1) is called;
[11] X(=10) is not < 10, so WriteLn('Recursion Function #',X);
[12] Exit Function and return to point where function was called
[13] X(=09)WriteLn('Recursion Function #',X);
[14] Exit Function and return to point where function was called
[15] X(=08)WriteLn('Recursion Function #',X);
[...]
[26] Exit Function and return to point where function was called
[27] X(=02)WriteLn('Recursion Function #',X);
[28] Exit Function and return to point where function was called
[29] X(=01)WriteLn('Recursion Function #',X);
[30] Done!

Your output would be:
  Recursion Function #10
  Recursion Function #9
  Recursion Function #8
  Recursion Function #7
  Recursion Function #6
  Recursion Function #5
  Recursion Function #4
  Recursion Function #3
  Recursion Function #2
  Recursion Function #1


Hope this explains it a bit.
Phat Nat




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.