[comp.os.cpm] COMPAS/Turbo/recursion program

binder@decvax.DEC.COM (A complicated and secret quotidian existence) (07/02/88)

PROGRAM Tryit (INPUT, OUTPUT);
 
{
Recursion program discussed earlier in INFO-CPM Digest - implemented
for Turbo Pascal.
 
The program as originally submitted will not work in *any* correctly
implemented Pascal, because it allows only one byte for the variable
"nextchar".   Declaring the variable inside a procedure doesn't mean
that the variable will have a new space created for it on every call
to the procedure; it only means that the variable is not global.
 
Furthermore, it is necessary to make your Pascal's I/O routines take
characters as they are typed, instead of waiting for a <CR>.   Turbo
does this by using a file read from predeclared file "Kbd".  Then it
is necessary to write the character because file reads do not echo.
}
 
{$A-}
 
{
The above compiler directive enables recursion in the CP/M-80 incar-
nation of Turbo.
}
 
VAR
        index:          INTEGER;
 
        nextchar:       ARRAY [1..100] OF CHAR;  { Allow 100 chars }
 
PROCEDURE Readwrite;
 
BEGIN
  index := index+1;
  READ (Kbd, nextchar [index]);
  WRITE (nextchar [index]);
  IF (nextchar [index] <> ' ') THEN Readwrite;
  WRITE (nextchar [index]);
  index := index-1
END;
 
BEGIN
  index := 0;
  WRITE ('Enter a word, end with a space: ');
  Readwrite
END.

ken@cs.rochester.edu (Ken Yap) (07/03/88)

|The program as originally submitted will not work in *any* correctly
|implemented Pascal, because it allows only one byte for the variable
|"nextchar".   Declaring the variable inside a procedure doesn't mean
|that the variable will have a new space created for it on every call
|to the procedure; it only means that the variable is not global.

I beg your pardon? Distinct instances of called procedures should have
their own instances of local variables, otherwise recursion is
handicapped.  Any Pascal compiler that doesn't implement this correctly
is brain-damaged. (I know there are some micro compilers out there that
require recursive procedures to be identified as such, so that they can
get away with allocating static storage for local variables instead of
stack storage, because stack manipulation is expensive on itty-bitty
micros.) The relevant reference is J&W 3rd edition, paragraph 10.3.

The reason why the program posted won't work as such is because of line
buffering on most systems.

	Ken