Scope of Definitions

Any items defined within a subprocedure are local to the subprocedure. If a local item is defined with the same name as a global data item, then any references to that name inside the subprocedure use the local definition.

However, keep in mind the following:

When using a global KLIST or PLIST in a subprocedure some of the fields may have the same names as local fields. If this occurs, the global field is used. This may cause problems when setting up a KLIST or PLIST prior to using it.

For example, consider the following source.

Figure 6. Scope of Key Fields Inside a Module
      * Main procedure definitions
     D Fld1            S              1A
     D Fld2            S              1A

      * Define a global key field list with 2 fields, Fld1 and Fld2
     C     global_kl     KLIST
     C                   KFLD                    Fld1
     C                   KFLD                    Fld2

      * Subprocedure Section
     P Subproc         B
     D Fld2            S              1A

      * local_kl has one global kfld (fld1) and one local (fld2)
     C     local_kl      KLIST
     C                   KFLD                    Fld1
     C                   KFLD                    Fld2

      * Even though Fld2 is defined locally in the subprocedure,
      * the global Fld2 is used by the global_kl, since global KLISTs
      * always use global fields.  As a result, the assignment to the
      * local Fld2 will NOT affect the CHAIN operation.

     C                   EVAL      Fld1 = 'A'
     C                   EVAL      Fld2 = 'B'
     C     global_kl     SETLL     file

      * Local KLISTs use global fields only when there is no local
      * field of that name.  local_kl uses the local Fld2 and so the
      * assignment to the local Fld2 WILL affect the CHAIN operation.
     C                   EVAL      Fld1 = 'A'
     C                   EVAL      Fld2 = 'B'
     C     local_kl      SETLL     file
      ...
     P                 E

For more information on scope, see Scope of Definitions.



[ Top of Page | Previous Page | Next Page | Contents | Index ]