{{fbdoc item="title" value="CONST (Qualifier)"}}----
Specifies that a data type or pointer data type is read only.

{{fbdoc item="syntax"}}##
	... [[KeyPgAs|as]] [**Const**] //[[DataType|datatype]]// [ [**Const**] [[KeyPgPtr|ptr]] ... ]
##
{{fbdoc item="param"}}
	##//datatype//##
		Name of a standard or user defined data type.

{{fbdoc item="desc"}}
	Specifies that the ##//datatype//## or ##[[KeyPgPtr|ptr]]## immediately to the right of the ##**Const**## qualifier is to be considered as read only.  Read-only (##**Const**##) declarations are a measure of type safety that can be read as 'promises not to change.'  The compiler uses the const declarations to check operations on variables and parameters and generate an error at compile time if their data could potentially change.  There is no runtime overhead for using ##**Const**## qualifiers since all of the checks are made at compile time.
	
	##**Const**## can be used anywhere data type declarations are made.  This includes variables, parameters, function return results, user defined type fields, type aliases, and casting.  The ##//datatype//## can be any built-in standard data type or user defined type.
	
	Read-only variables must have an initializer since modifying a read-only variable through an assignment will generate a compiler error.  The initializer may appear after the declaration of the variable.
	
	Both non-const and const variables may be passed to a procedure expecting a const parameter.  However, a const variable may not be passed to a procedure taking a non-const parameter, and will generate a compile error.
	
	Procedures can be overloaded based on the const-ness of parameters.  For example a procedure can be overloaded where one version of the procedure takes a ##'byref foo as bar'## parameter and another version of the procedure takes a ##'byref foo as const bar'## parameter.
	
	With pointer declarations, ##**Const**## can be used to indicate which part of the pointer declaration is read-only (all other parts are by default read-write).  The read-only portion of the pointer data type could be the pointer itself (the address), what the pointer points to (the data), or both.  In a declaration with more than one level of ##[[KeyPgPtr|Ptr]]## indirection, the right most ##[[KeyPgPtr|Ptr]]## indicates the highest order level of indirection and is therefore dereferenced first.
	
	The compiler has an internal hard-limit of eight (8) levels of pointer indirection with respect to const qualifiers and the behavior of using ##**Const**## with ##[[KeyPgPtr|Ptr]]## data types having greater than eight (8) levels of indirection is undefined.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/datatype/const-var.bas"}}%%(freebasic)
'' Const Variables

'' procedure taking a const parameter
sub proc1( byref x as const integer )

  '' can't change x because it is const
  '' x = 10 '' compile error

  '' but we can use it in expressions and
  '' assign it to other variables
  dim y as integer
  y = x
  y = y * x + x

end sub

'' procedure taking a non-const parameter
sub proc2( byref x as integer )
  '' we can change the value
  x = 10
end sub

'' declare a non-const and const variable
dim a as integer
dim b as const integer = 5

'' proc1() will accept a non-const or const
'' argument because proc1() promises not to
'' change the variable passed to it.
proc1( a )
proc1( b )

'' proc2() will accept a non-const argument
proc2( a )

'' but not a const argument because proc2()
'' might change the variable's data and we
'' promised that 'b' would not change.
'' proc2( b ) '' compile error
%%

{{fbdoc item="filename" value="examples/manual/datatype/const-ptr.bas"}}%%(freebasic)
'' Const Pointers

'' an integer
dim x as integer = 1
dim y as integer = 2
dim z as integer = 3

'' To check that the compiler generates errors
'' when attempting to reassign const variables,
'' uncomment the assignments below.

''
scope
  '' a pointer to an integer
  dim p as integer ptr = @x

  p = @y       /' OK - pointer can be changed '/
  *p = z       /' OK - data can be changed '/

end scope

''
scope
  '' a pointer to a constant integer
  dim p as const integer ptr = @x

  p = @y       /' OK - pointer can be changed '/
  '' *p = z    /' Error - data is const '/

end scope

''
scope
  '' a constant pointer to an integer
  dim p as integer const ptr = @x

  '' p = @y    /' Error - pointer is const '/
  *p = z       /' OK - data can be changed '/

end scope

''
scope
  '' a constant pointer to a constant integer
  dim p as const integer const ptr = @x

  '' p = @y    /' Error - pointer is const '/
  '' *p = z    /' Error - data is const '/

end scope
%%

{{fbdoc item="filename" value="examples/manual/datatype/const-ovl.bas"}}%%(freebasic)
'' Const Parameters in an Overloaded Procedure

'' procedure with non-const parameter
sub foo overload( byref n as integer )
  print "called 'foo( byref n as integer )'"
end sub

'' procedure with const parameter
sub foo overload( byref n as const integer )
  print "called 'foo( byref n as const integer )'"
end sub

dim x as integer = 1
dim y as const integer = 2

foo( x )
foo( y )

'' OUTPUT:
'' called 'foo( byref n as integer )'
'' called 'foo( byref n as const integer )'
%%
{{fbdoc item="diff"}}
	- New to ""FreeBASIC""

{{fbdoc item="see"}}
	- ##[[KeyPgConst|Const]]##
	- ##[[KeyPgConstMember|Const (Member)]]##
	- ##[[KeyPgDim|Dim]]##
	- ##[[KeyPgType|Type]]##

{{fbdoc item="back" value="CatPgStdDataTypes|Standard Data Types"}}