Local variables are scoped to a PAscript object. Therefore, you can only access local variables within the block of code that defines the PAscript object.

Local variables may or may not correspond to a PAtools object. To define local variables in PAscript, you can use data types that have no equivalent in the PAtools name types.

The syntax for defining local variables is:

type var_1 [=initialization], var_2 [=initialization]...
type var_1, var_2, .... , var_n

Where you can use one of the following simple data types as the type:

  • int32 , int16 , int8—Signed integer
  • uint32 , uint16 , uint8—Unsigned integer
  • int32[] , int16[] , int8[]—Field with signed integers
  • uint32[] , uint16[] , uint8[]—Field with unsigned integers
  • real64 , real32—Floating-point number
  • real64[] , real32[]—Field with floating-point numbers
  • text—Text
  • buffer—Field with any content
  • In addition to the simple data types, corresponding data types for various other PAtools name types are available in PAscript. Refer to Data Types for information about other PAscript data types.

    PAscript distinguishes the following types of local variables:

    • Program local variables
    • Object local variables
    • Function local variables

    Program Local Variables

    Program local variables are only valid within a PAscript program.

    Access to program local variables is not possible outside of the PAscript program in which the variables are defined. Program local variables are static. Therefore, program local variables retain their value throughout the execution of the program.

    Program local variables are defined after the section globals code block. If there is no section globals code block, the variables are defined immediately after the keyword program. It is not possible to define program local variables anywhere in the code.

    Object Local Variables

    Object local variables are defined within a PAscript library or within a PAscript class.

    If the private keyword precedes a variable definition, the variable scopes to that library or class. If you do not use the private keyword, the variable scopes to the PAscript program that uses that library or class.

    NI does not recommend making object local variables public. A better practice is to create public Get/Set function wrappers around the variable that your PAscript program requires access to.

    Function Local Variables

    Local variables defined in a PAscript function are only valid in this function.

    The value of a function local variable can be returned from the function if it is required in another part of the PAscript program.

    Initialization Code or Value

    The initialization code or value of a simple local variable is optional. If there is no initialization value present, the variable is pre-initialized with 0 or "", depending on the type.

    • You can use previously declared variables in the initialization code.
    • Specify the length for texts and buffers.
    • Specify dimensioning for fields and value tables.
    • Enter comments using // or /* ... */ to the right of the variables.
    • Local variables are optional.

    Example

    program
         section globals
             int32 a, b
             int32 c
             real64 d
             pabasic pabasicProgram
         endsection
     
         int32 local = a * b
         real64 var2  // comment for var2
         real64 var3 =  sin (PI/2)
    endprogram