Start of change

%SIZE built-in function

The %SIZE built-in function returns the number of bytes occupied by the CL variable.

The %SIZE built-in function can be used anywhere that CL supports an arithmetic expression. %SIZE can be used alone or as part of a more complex arithmetic expression. For example, %SIZE can be used to compare the number of bytes used by two CL variables in the COND parameter of an IF or WHEN command. %SIZE can also be used to set the value of a CL command parameter, if the associated command object defines the parameter with EXPR(*YES) and TYPE of *DEC, *INT2, *INT4, *UINT2, or *UINT4.

The format of the getting size built-in function is:
%SIZE(variable-argument)

The variable-argument must be a CL variable.

The following are examples of using the %SIZE built-in function:

  • Get the size of numeric variables
    DCL VAR(&NUM1) TYPE(*DEC)
    DCL VAR(&NUM2) TYPE(*DEC) LEN(7 2)
    DCL VAR(&NUM3) TYPE(*INT)
    DCL VAR(&NUM4) TYPE(*UINT) LEN(8)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 8. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM1))
    /* &RTN will have the value 4. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM2))
    /* &RTN will have the value 4. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM3))
    /* &RTN will have the value 8. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&NUM4))
  • Get the size of character variables
    DCL VAR(&CHAR1) TYPE(*CHAR)
    DCL VAR(&CHAR2) TYPE(*CHAR) LEN(20)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 32. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR1))
    /* &RTN will have the value 20. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR2))
    /* &RTN will have the value 52. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&CHAR1) + %SIZE(&CHAR2))
  • Get the size of logical variables
    DCL VAR(&LGL1) TYPE(*LGL)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 1. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&LGL1))
  • Get the size of pointer variables
    DCL VAR(&PTR1) TYPE(*PTR)
    DCL VAR(&RTN) TYPE(*INT) LEN(2)
    
    /* &RTN will have the value 16. */
    CHGVAR VAR(&RTN) VALUE(%SIZE(&PTR1))
End of change