{{fbdoc item="title" value="UBOUND"}}----
Returns the upper bound of an array's dimension

{{fbdoc item="syntax"}}##
	[[KeyPgDeclare|declare]] [[KeyPgFunction|function]] **Ubound** ( array() [[KeyPgAs|as]] [[KeyPgAny|any]], [[KeyPgByval|byval]] //dimension// [[KeyPgAs|as]] [[KeyPgInteger|integer]] = 1 ) [[KeyPgAs|as]] [[KeyPgInteger|integer]]
##
{{fbdoc item="usage"}}##
	//result// = **Ubound**( //array// [, //dimension// ] )
##
{{fbdoc item="param"}}
	##//array//##
		an array of any type
	##//dimension//##
		the dimension to get upper bound of

{{fbdoc item="ret"}}
	Returns the upper bound of an array's dimension.
 
{{fbdoc item="desc"}}
	##**Ubound**## returns the largest value that can be used as an index into a particular dimension of an array.
	
	Array dimensions are numbered from one (##1##) to ##//n//##, where ##//n//## is the total number of dimensions. If ##//dimension//## is not specified, ##**Ubound**## will return the upper bound of the first dimension.
	
	If ##//dimension//## is zero (0), ##**Ubound**## returns ##//n//##, the number of dimensions in the array. For any other ##//dimension//## values outside of the valid range ##1##..##//n//##, the result is ##-1##. This can be used to detect the number of dimensions of variable-length arrays, and in combination with the result of ##Lbound()## for such cases, whether a given dimension exists, or whether the array is empty (zero dimensions). See the ##[[KeyPgLbound|Lbound]]## page for more information.

{{fbdoc item="ex"}}

{{fbdoc item="filename" value="examples/manual/array/ubound.bas"}}%%(freebasic)
dim array(-10 to 10, 5 to 15, 1 to 2) as integer

print ubound(array) 'returns 10
print ubound(array, 2) 'returns 15
print ubound(array, 3) 'returns 2
%%

{{fbdoc item="filename" value="examples/manual/array/ubound2.bas"}}%%(freebasic)
'' determining the size of an array
Dim As Short array(0 To 9)
Dim As Integer arraylen, arraysize

arraylen = UBound(array) - LBound(array) + 1
arraysize = arraylen * SizeOf( Short )

Print "Number of elements in array:", arraylen    '10
Print "Number of bytes used in array:", arraysize '10 * 2 = 20 
%%

{{fbdoc item="filename" value="examples/manual/array/ubound3.bas"}}%%(freebasic)
'' determining the size of a multi-dimensional array
Dim As Long array4D(1 To 2, 1 To 3, 1 To 4, 1 To 5)
Dim As Integer arraylen, arraysize


arraylen = (UBound(array4D, 4) - LBound(array4D, 4) + 1) _
		 * (UBound(array4D, 3) - LBound(array4D, 3) + 1) _
		 * (UBound(array4D, 2) - LBound(array4D, 2) + 1) _
		 * (UBound(array4D, 1) - LBound(array4D, 1) + 1)

arraysize = arraylen * SizeOf( Long )

Print "Number of elements in array:", arraylen    '2 * 3 * 4 * 5 = 120
Print "Number of bytes used in array:", arraysize '120 * 4 = 480
%%

{{fbdoc item="filename" value="examples/manual/array/ubound4.bas"}}%%(freebasic)
'' determining whether an array is empty
dim array() as integer

print "lbound: "; lbound( array ), "ubound: "; ubound( array )  '' 1 and 0

if lbound( array ) > ubound( array ) then
	print "array is empty"
else
	print "array is not empty"
end if
%%

{{fbdoc item="filename" value="examples/manual/array/ubound5.bas"}}%%(freebasic)
sub printArrayDimensions( array() as integer )
	print "dimensions: " & ubound( array, 0 )

	'' For each dimension...
	for d as integer = lbound( array, 0 ) to ubound( array, 0 )
		print "dimension " & d & ": " & lbound( array, d ) & " to " & ubound( array, d )
	next
end sub

dim array() as integer
printArrayDimensions( array() )

print "---"

redim array(10 to 11, 20 to 22)
printArrayDimensions( array() )
%%

{{fbdoc item="see"}}
	- ##[[KeyPgLbound|Lbound]]##
	- ##[[KeyPgStatic|Static]]##
	- ##[[KeyPgDim|Dim]]##
	- ##[[KeyPgRedim|Redim]]##
	- ##[[KeyPgSizeof|Sizeof]]##

{{fbdoc item="back" value="CatPgArray|Array Functions"}}