[fpc-pascal] Static local variables available?

Sven Barth pascaldragon at googlemail.com
Thu Jul 20 11:57:16 CEST 2017


On 20.07.2017 11:11, Maciej Izak wrote:
> 2017-07-20 11:03 GMT+02:00 Bo Berglund <bo.berglund at gmail.com
> <mailto:bo.berglund at gmail.com>>:
> 
>     So since I don't really want to use a global, is it possible to
>     declare a local variable static in the sense that it retains its
>     values across calls to the procedure?
>     If so how is it done?
> 
> 
> procedure foo;
> {$PUSH}
> const{$J+}
>   s : string ='';
> {$POP}
> begin
>   writeln(s);
>   if s = '' then
>     s := 'hello from static local variable';
> end;
> 
> begin
>   foo; // will print empty line
>   foo; // will print 'hello from static local variable'
>   foo; // will print 'hello from static local variable'
> end.

For completeness sake:
https://www.freepascal.org/docs-html/current/ref/refse10.html#x22-210002.2
, especially the remark at the end.

Regards,
Sven




More information about the fpc-pascal mailing list