Routines (alphabetical) > Routines: N > N_ELEMENTS

# N_ELEMENTS

The N_ELEMENTS function returns the number of elements contained in an expression or variable.

## Examples

### Example 1

This example finds the number of elements in an array:

; Create an integer array:
I = INTARR(4, 5, 3, 6)
; Find the number of elements in I and print the result:
PRINT, N_ELEMENTS(I)

### Example 2

A typical use of N_ELEMENTS is to check if an optional input is defined, and if not, set it to a default value:

IF (N_ELEMENTS(roo) EQ 0) THEN roo=rooDefault

The original value of `roo` may be altered by a called routine, passing a different value back to the caller. Unless you intend for the routine to behave in this manner, you should prevent it by differentiating N_ELEMENTS’ parameter from your routine’s variable:

IF (N_ELEMENTS(roo) EQ 0) THEN rooUse=rooDefault $

ELSE rooUse=roo

### Example 3

This example finds the number of elements in a list:

list = LIST('zero', 1, 2.0)
PRINT, N_ELEMENTS(list)

## Syntax

Result = N_ELEMENTS(Expression)

## Return Value

Returns the number of elements.

## Arguments

### Expression

The expression for which the number of elements is to be returned. The number of elements in an array is equal to the product of its dimensions. If Expression is an undefined variable, N_ELEMENTS returns zero.

Note: If Expression is an instance of an object class that overloads the IDL_Object::_overloadSize method, the value returned by that method will be used to determine the number of elements. See IDL_Object for details.

## Keywords

None.

## Version History

Original | Introduced

## See Also

N_TAGS