[fpc-pascal] Result: string

Michael Van Canneyt michael at freepascal.org
Mon Mar 4 22:16:27 CET 2013



On Mon, 4 Mar 2013, Howard Page-Clark wrote:

> On 04/03/13 8:23, Juha Manninen wrote:
>> On Mon, Mar 4, 2013 at 9:21 PM, José Mejuto <joshyfun at gmail.com> wrote:
>>> What's the expected output of this code ?
>>> 
>>> function TheA(): string;
>>> begin
>>>    Result:=Result+'A';
>>> end;
>>> 
>>> writeln(TheA());
>>> 
>>> I thought that when the result type is an automated one its value gets
>>> initialized... Maybe I'm wrong...
>> 
>> Yes you are wrong. It is very illogical because a local string
>> variable is initialized to be empty but the return value is not.
>> Delphi has the same problem.
>> 
>> I once made even a report about it:
>>    http://bugs.freepascal.org/view.php?id=20907
>
> That report says the issue was assigned to Jonas and fixed in revision 20427 
> (ver 2.6.1).
> I find the current release (2.6.2) initialises a string function result to 
> EmptyStr as you would hope.

{$mode objfpc}
{$h+}

function TheA(): string;

begin
   Result:=Result+'A';
end;

Var
   C : String;

begin
   C:='B';
   C:=TheA;
   C:=TheA;
   Writeln(C);
end.

Prints AA

Michael.


More information about the fpc-pascal mailing list