2018-03-24 20:49 GMT+01:00 Ondrej Pokorny <laza...@kluug.net>: > Does it mean I can assume a local string variable is always (=whenever the > routine is called) initialized to ''? I.e. that TestB always returns 'a'? > > function TestB: string; > var B: string; > begin > B := B + 'a'; > Result := B; > end; > > Until now I though that the above code is unsafe and I always initialized > all local variables (also managed) before use. I assumed the initialization > to empty string does not need to happen at every routine call. >
Please note that string/dynamic array as Result is exception (it works like *var* parameter). IMO this is the ugliest Pascal behavior ever: === example begin === function test1: string; begin Result := Result + 'a'; end; function test2: TArray<byte>; begin SetLength(Result, Length(Result) + 1); end; begin writeln(test1); // print a writeln(test1); // print aa writeln(test1); // print aaa writeln(test1); // print aaaa writeln(Length(test2)); // print 1 writeln(Length(test2)); // print 2 writeln(Length(test2)); // print 3 writeln(Length(test2)); // print 4 end. === example end === -- Best regards, Maciej Izak
_______________________________________________ fpc-devel maillist - fpc-devel@lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel