: 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