Pascal

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

This Forum Only

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
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??
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);
[13] X(=09)WriteLn('Recursion Function #',X);
[15] X(=08)WriteLn('Recursion Function #',X);
[...]
[27] X(=02)WriteLn('Recursion Function #',X);
[29] X(=01)WriteLn('Recursion Function #',X);
[30] Done!

```  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