SCOPE


As illustrated in Figure 6.1, a source file containing the mainline RPG program code (mainline procedure) includes all global data. This includes fields, arrays, data structures, constants, files, input fields, and output fields.

The term scope is used to define the limit of visibility of a program item such as a field. For example, all fields have a scope based on the program structure. A field has a scope, which is global or local, that limits visibility of the field as follows:

Scope

Visibility

Local

Subprocedure

Global

Source file/module

If a subprocedure is specified, it has access to all global variables within the same source file. In addition, it has its own local variables. Local variables are available only to the subprocedure in which they are declared. Figure 6.4 illustrates the scope of variable definitions within RPG IV modules and subprocedures. Remember, for purposes of this text, the term source file means AS/400 source file member.

click to expand
Figure 6.4: Scope of global and local variables.

Local variables of the same name as a global variable are supported in RPG. In this situation, within the subprocedure, the local variable has precedence over the global variable. The properties of the local and global variables of the same name need not be similar. For example, a global field named ITEM could be defined as a 10-position character field within the mainline procedure. Within a subprocedure, the ITEM field (such as a five-digit packed decimal field) can be defined differently. Typically, this kind of situation is avoided through program design.

All global variables within a source file are visible only to the mainline procedure and the subprocedures that are embedded in that source file. For example, an application is made up of two source files named MYAPP and TOOLS. The source file named MYAPP contains a global variable named CUSTNAME. Subprocedures included in the MYAPP source file have access to CUSTNAME.

The mainline procedure (if one exists) and all subprocedures specified in the TOOLS source file do not have access to the CUSTNAME field.




The Modern RPG IV Language
The Modern RPG IV Language
ISBN: 1583470646
EAN: 2147483647
Year: 2003
Pages: 156
Authors: Robert Cozzi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net