Tricky P Question
Author Message
Tricky P Question

Hello fellow darksiders.
Here's a weird one for you. I am actually using THINK Pascal for the
Mac, but the question is quite general. Suppose that I declare a
recursive proc that has the standard if else structure (If condition
dosomething else call again(with new data). What happens if at some
level of nesting I call Exit(proc)??????? Will ALL the stack frames get
deallocated (in my Mac its LINK/UNLK) or just the last one? I guess what
I am asking is WHERE the proc will exit? Just before the last call or
just after the first call? Couldn't figure this one out, although I
suspect that the Exit statement deallocates the whole thing.
Any thoughts are welcome. Thanx in advance.
--
*********************************************
* The strangest meme that keeps coming      *
* back to my mind is the following peculiar *
* application of the transitive law:        *
* "I admire Bach" and "Bach admired Christ",*
* thus "I admire Christ", independent of    *
* any actual beliefs of mine on the subject.*
*********************************************

Sun, 27 Feb 2000 03:00:00 GMT
Tricky P Question

] Hello fellow darksiders.
] Here's a weird one for you. I am actually using THINK Pascal for the
] Mac, but the question is quite general. Suppose that I declare a
] recursive proc that has the standard if else structure (If condition
] dosomething else call again(with new data). What happens if at some
] level of nesting I call Exit(proc)??????? Will ALL the stack frames
] get deallocated (in my Mac its LINK/UNLK) or just the last one? I
] guess what I am asking is WHERE the proc will exit? Just before the
] last call or just after the first call? Couldn't figure this one out,
] although I suspect that the Exit statement deallocates the whole
] thing. Any thoughts are welcome. Thanx in advance.

It exits ONLY the most recent iteration.

Create a new project and enter this program.
PROGRAM test;
VAR
i: integer;
PROCEDURE testrecur;
BEGIN
i := SUCC(i);
IF i < 10 THEN
testrecur;
writeln(i);
i := PRED(i);
exit(testrecur);
END;
BEGIN
i := 0;
testrecur;
ShowText;
END.

It will print out
10
9
8
7
6
5
4
3
2
1

Although why you didn't test this yourself I don't know.

Gods I hope this isn't another homework assignment that somebody can't
be bothered to do!

--
John Moreno

Sun, 27 Feb 2000 03:00:00 GMT
Tricky P Question

You will find that the exit will purely return you to the last recursive
call you made, take the following example...

Function Factorial(Number: Integer): Integer;
begin
if Number=1 Then
Factorial:=1
else
Factorial:=Number * Factorial(Number-1);
end;

If you changed it to:

Function Factorial(Number: Integer): Integer;
begin
if Number=1 Then
begin
Factorial:=1;
exit;
end
else
Factorial:=Number * Factorial(Number-1);
end;

it should behave exactly the same...

Quote:
> Hello fellow darksiders.
> Here's a weird one for you. I am actually using THINK Pascal for the
> Mac, but the question is quite general. Suppose that I declare a
> recursive proc that has the standard if else structure (If condition
> dosomething else call again(with new data). What happens if at some
> level of nesting I call Exit(proc)??????? Will ALL the stack frames get
> deallocated (in my Mac its LINK/UNLK) or just the last one? I guess what
> I am asking is WHERE the proc will exit? Just before the last call or
> just after the first call? Couldn't figure this one out, although I
> suspect that the Exit statement deallocates the whole thing.
> Any thoughts are welcome. Thanx in advance.

--
Daniel Silverstone (Usually ;)

Look at the farstar Homepages on:
http://www.farstar.demon.co.uk/

Try out Daniel's TPascal Page & Delphi 2.0 bits at
http://www.farstar.demon.co.uk/Pascal/index.htm

Mail me & give comments (I know it's sparse ;)

Mon, 28 Feb 2000 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages