Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-11

Is there a way to declare a static varible in a function body?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

yu ping

Is there a way to declare a static varible in a function body? the varible should remain the value assigned from last call.

thanks

Reply by email

Frank Peelo
to yu ping
yu ping wrote
Is there a way to declare a static varible in a function body? the varible should remain the value assigned from last call.

Typed constant, const /name/ : /type/ = /initial value/; Takes the initial value when the program starts, but you can assign new values to it. But the value remains constant between calls.

e.g.

Function NextI:Integer;
const
   NextValue:Integer = 0;
Begin
   NextI := NextValue;
   NextValue := NextValue+1;
end;
var
   x : Integer;
Begin
   for x := 1 to 10 do
     writeln(NextI);
end.

Reply by email

yu ping
to Frank Peelo

Thanks, you are realy clever:))

2009/11/18 Frank Peelo

Show quoted text 32 lines
On 18/11/2009 08:10, yu ping wrote: > Is there a way to declare a static varible in a function body? the varible > should remain the value assigned from last call. > Typed constant, const /name/ : /type/ = /initial value/; Takes the initial value when the program starts, but you can assign new values to it. But the value remains constant between calls. e.g. Function NextI:Integer; const NextValue:Integer = 0; Begin NextI := NextValue; NextValue := NextValue+1; end; var x : Integer; Begin for x := 1 to 10 do writeln(NextI); end. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email