Help: Don't understand this pascal code lazarus / 13-General ---------------------------------------------------------------------- rabiul · 2008-05-02 09:32 Hello, I am  learning pascal and found a program in the book now I am reading ( Mastering Turbo Pascal, author: Tom Swan ). It is an example of 'mutual recursion'. I think after executed, the result should be only 'Z', but this is not happening. And I can not understand the result. The author mentioned 'Although the example may seem silly, it demonstrates a real programming situation that often occurs in Pascal. ....... To understand how the program works, try to execute its statement by hand. The results, which you have to run the program to discover, may surprise you!'. And I did so and tried hard to understand, but could not get it. If someone understand this, please explain the result. Thanks a lot in advance. The code I have compiled (with fpc) is: Code: [Select] Program RecurseProc; Procedure B ( Ch : Char ); Forward; Procedure A ( Ch : Char ); Begin if Ch < 'Z' then B(Ch); write(Ch) End; Procedure B ( Ch : Char ); Begin A ( Succ ( Ch ) ) End; Begin A( 'A' ); End. ---------------------------------------------------------------------- cyber_python · 2008-05-02 10:52 Quote Procedure B ( Ch : Char ); Forward; Here you declare a procedure B (but not define it yet) so that you can use it in procedure A. Quote Procedure A ( Ch : Char ); Begin if Ch < 'Z' then B(Ch); write(Ch) End; This procedure takes a character as a parameter and checks if it is lower than 'Z' (in fact it is testing their ordinal numbers). If it is not, it prints the character to the predefined output (screen), otherwise it calls procedure B and passes the character to it. ATTENTION : When B finishes, A prints the character to the predefined output. Quote Procedure B ( Ch : Char ); Begin A ( Succ ( Ch ) ) End; This procedure takes a character, finds the successor character and calls A for its value. Quote Begin A( 'A' ); End. Here, we have the actual program. We make a call to procedure A and we pass the character 'A'. Now proc A will test 'A' against 'Z' and because it is lower than 'Z' it will call B('A'). B('A') will call A('B'), then A('B') will call B('B'). B('B') will call A('C'), then A('C') will call B('C') and so on. The interesting part is when B('Y') calls A('Z'). A('Z') will test 'Z' against 'Z' and because their values are equal it prints 'Z' and finishes. B('Y') finishes and A('Y') continues execution prints 'Y' and finishes. B('X') finishes and A('X') continues execution prints 'X' and finishes. B('W') finishes and A('W') continues execution prints 'W' and finishes and so on. So, what the code does is print the list of letters in reverse like this: ZYXWVU.....CBA Hope this helps. Regards, cyber_python. ---------------------------------------------------------------------- rabiul · 2008-05-02 11:46 Dear cuber_python, Thank you very much for taking time to reply. Perhaps I am a little bit away from the complete understanding. I am telling you, Code: [Select] The interesting part is when B('Y') calls A('Z'). A('Z') will test 'Z' against 'Z' and because their values are equal it prints 'Z' and finishes. ... At the last time Procedure A sends B(Ch) which is B(Y) to Procedure B. Now Procedure B makes Y to Z and sends it back to Procedure A. Procedure checks if Ch is less than Z and found it is not and rather it is Z. So it does not send the to Procedure B. And it prints Z. It did not call Procedure B, as Ch was equal to Z and printed Z. So it should stop here. How does this print Y (and all other alphabets) after Z? How does Y goes upto writeln without being checked by 'if Ch < 'Z' then' ... of Procedure A? Hope you have get the point. ---------------------------------------------------------------------- theo · 2008-05-02 12:31 http://en.wikipedia.org/wiki/Recursion_%28computer_science%29#Function _2_with_swapped_lines ---------------------------------------------------------------------- arnoldb · 2008-05-02 13:01 Hi Rabiul I'ts difficult to just put it in words, but if you add a line right at the start of procedure A& B like "writeln ('Executing Proc A - Ch ='+Ch);", it should become clearer how the recursion works.   Try this in a shell / command prompt with a fairly big scroll-back buffer, or divert standard out to a file, as it will print a lot of lines. It basically creates loops within loops.  If you want it a bit quicker/shorter try using 'E' instead of 'Z' Program RecurseProc; Procedure B ( Ch : Char ); Forward; Procedure A ( Ch : Char ); Begin writeln ('Starting Procedure A - Ch ='+Ch); if Ch < 'Z' then B(Ch); writeln ('In Procedure A, after Ch