Q(uick)BASIC Function: UBOUND

Quick View

UBOUND

A memory function that returns the upper bound (largest available subscript) for the indicated dimension of an array

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • LBOUND(array[,dimension%])
  • UBOUND(array[,dimension%])
Description/Parameter(s)
array The name of the array.
dimension% Indicates the array dimension whose lower or upper bound is returned. Use 1 for the first dimension, 2 for the second dimension, etc. The default is 1.
Example
DIM a%(1 TO 3, 2 TO 7) PRINT LBOUND(a%, 1), UBOUND(a%, 2)

See also:

Syntax
  • UBOUND(array[,dimension])
Description/Parameter(s)
array identifies the array
dimension an numeric-expression that has an integer value, indicates the dimension of the array for which you want to see the smallest available subscript. Use 1 for the first dimension, 2 for the second dimension, etc. This is an optional argument for one-dimensional arrays.

The argument dimension is an integer from 1 to the number of dimensions in the array. For an array dimensioned as follows, UBOUND returns the values listed below:

  • DIM A(1 TO 100, 1 TO 50, -3 TO 4)
Invocation Value Returned
UBOUND(A,1) 100
UBOUND(A,2) 50
UBOUND(A,3) 4

You can use the shortened syntax UBOUND(array) for one-dimensional arrays since the default value for dimension is 1.

Use the LBOUND function to find the lower limit of an array dimension.

Example

UBO_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the UBOUND and LBOUND functions. To look at the program in the View window and, optionally, to run it, load the program using the File menu's Open Program command.
The program shows how LBOUND and UBOUND can be used together in a SUB to determine the size of an array passed to the SUB by a calling program.

See also:

Syntax
  • UBOUND(array[,dimension%])
Description/Parameter(s)
array The name of the array variable to be tested.
dimension% An integer ranging from 1 to the number of dimensions in array; indicates which dimension's upper bound is to be returned. Use 1 for the first dimension, 2 for the second dimension, etc. This argument is optional for one-dimensional arrays.

Usage Notes

  • The UBOUND function is used with the LBOUND function to determine the size of an array.
  • UBOUND returns the values listed below for an array with the following dimensions:
    • DIM A(1 TO 100, 1 TO 3, -3 TO 4)
    InvocationValue returned
    UBOUND(A,1)100
    UBOUND(A,2)3
    UBOUND(A,3)4
  • You can use the shortened syntax UBOUND(array) for one-dimensional arrays because the default value for dimension% is 1.
  • Use the LBOUND function to find the lower limit of an array dimension.
Example

This example uses the LBOUND and UBOUND functions in a SUB procedure to determine the size of an array passed to the procedure by a calling program.

DECLARE SUB PRNTMAT (A!()) DIM A(0 TO 3, 0 TO 3) FOR I% = 0 TO 3 FOR J% = 0 TO 3 A(I%, J%) = I% + J% NEXT J% NEXT I% CALL PRNTMAT(A()) END SUB PRNTMAT (A()) STATIC FOR I% = LBOUND(A, 1) TO UBOUND(A, 1) FOR J% = LBOUND(A, 2) TO UBOUND(A, 2) PRINT A(I%, J%); " "; NEXT J% PRINT : PRINT NEXT I% END SUB