[fpc-devel]let Pascal stay Pascal

S. Schicktanz Sieghard.Schicktanz at t-online.de
Sun Oct 29 16:22:09 CET 2000


Hello!

Sorry for my messing up with this thread, especially since this is my first post here and I'm not even arguing 
FPC in the first place.

But anyway, I'd like to say a couple of words on the following:
At 27.10.2000 13:31:00, Jonas Maebe wrote:
>>var
>>  a: Integer;
>>
>>function fact: Integer;
>>begin
>>  fact := 1;
>>  if a <> 0 then
>>  begin
>>    Dec(a);
>>    fact := (a+1)*fact;
>>  end;
>>end;
>>
>>begin
>>  a := 3;
>>  Writeln(fact);
>>end.
>>
>>Of course it will write 6, and give the variable "a" value 0. Not in PPC.

This is a citation from a previous message - and what it says is plain wrong, IMHO.
This way of using a recursive function can only work occasionally, as it DEPENDS on the order of evaluation 
of the operands of an expression - which is to my knowledge NOT defined in (even With's original) 
definition of Pascal.
So this function MAY return the correct factorial of the entry value of a, it may return 0, or it may even 
return something weird, if by any means the oder of evaluation might change in midstream.

But you didn't even respond to THIS shortcoming, but wrote:

>It does if you use -So (Turbo Pascal compatibility mode) or -Sd (Delphi 
>compatibility more). You indeed have to write
>
>fact := (a+1)*fact();
>
>if you want "fact" to be called again in FPC or Obj-FPC mode, which is 
>indeed quite C-like. One reason is that we also introduced the C++ 
>feature "function overloading". So if you have
[deleted]

I disagree.
Even with overloaded functions, the compiler CAN tell from the side of an assignment whether to CALL the 
function or to assign to it's result variable:

IF the function appears on the right side of an assignment
  THEN call it { if this can possibly be done from syntax }
  ELSE assign to it's result variable.

This might need a little refinement for functions returning pointers, which is an extension to Pascal though 
and where it would make real sense to present something new (like using <function name>() for a 0-
parameter function to call on the left side of an assignment). Maybe even this is not needed, because to 
assign to the result of a recursive call of a function, you have to use the dereference operator (^) to give 
the compiler enough of a clue that it has to CALL the function instead of assigning to it's result variable.
So the strategy for recursive function usage might be formulated as following:

IF the function appears on the right side of an assignment
  THEN call it { if this can possibly be done from syntax }
  ELSE
     IF the function appears as part of an expression,
     THEN call it { if this can possibly be done from syntax }
     ELSE assign to it's result variable.

This, of course, does not apply to function parameters (or variables), which is really what you presented:

>type
>  tf = function: longint;
>
>procedure p(f: tf);
>begin
>end;
>
>procedure p(l: longint);
>begin
>end;
>
>function f: longint;
>begin
>  p(f); 
>end;

Here you do need some means to tell apart recursive use and function call - but in the context of 
procedural parameters, which are an extension to Wirth's Pascal.

Thank you for reading.
--
-----------------------------------------------------------
MfG, S. Schicktanz
 - acht zwei fuenf vier sieben Achmuehle / Eurasburg
Sieghard.Schicktanz at t-online.de (derzeit noch)
-----------------------------------------------------------






More information about the fpc-devel mailing list