[fpc-pascal] Typed Constants vs. Variables

Jonas Maebe jonas.maebe at elis.ugent.be
Mon Apr 6 08:29:43 CEST 2009

On 06 Apr 2009, at 03:37, Richard Ward wrote:

> Where/why would one use a typed constant vs. a variable.  i.e.
> const
> 	myConst : double = 2.0*Pi;
> var
> 	myVar : double = 2.0*Pi;
> It seems to me the typed constant is superfluous and can potentially  
> lead to bugs.

Typed constants predate the existence of initialized variables.  
a) typed constants are initialized once (when the program starts), and  
if changed keep their value until explicitly changed again (even if  
they are declared locally in a procedure/function, and over multiple  
independent invocations of such routines)
b) conversely, initialized variables are initialized every time their  
scope is activated (or whatever the proper term for that is: once in  
case they are declared in a program/unit scope, and every time a  
function/procedure is entered if they are declared locally in a routine)
c) typed constants can be made read-only by using the {$j-} switch


More information about the fpc-pascal mailing list