Introduction to NATURAL
02/07/2009
This document gives a basic introduction to NATURAL programming. Details what is NATURAL, NATURAL Data areas, NATURAL Variables and Statements.
Insurance / ISU 01 Lise Molineus Jaicy .P [email protected]
Introduction to NATURAL
What is NATURAL?
NATURAL is a 4th generation programming language. It is free format language where it can be either procedural or non-procedural. Can be used to build both online and batch programs. Databases like ADABAS and DB2 can talk to NATURAL. There are two modes of NATURAL programming, Structured and Reporting. The functional differences between structured mode and reporting mode are provided at the end of the document.
Defining Data in NATURAL
Types of Variables and Data Areas Defining Database Views Defining User Variables Redefinition Of Variables
Types of Variables:
Type Alphanumeric Numeric Packed decimal Control Logical Date Time Floating Point Integer Binary Format (Annn) (Nnn.m) (Pnn.m) (C) (L) (D) (T) (Fn) (In) (B) Size 1-253 characters 1-29 digits 1-29 digits 2-byte internal format 1-byte FALSE/TRUE P6 P12 4 bytes for single precision 8 bytes for double precision or 1,2 or 4 bytes 1-128 bytes
Internal Use
Introduction to NATURAL
Example: DEFINE DATA LOCAL 01 #NAME (A25)
01 #EMP-ID (N10) END-DEFINE
DATA Areas:
The data areas are used to maintain external NATURAL Data Definitions. Following are the three data areas.
LDA (Local Data Area) Data resides within the program and is used in the program itself. GDA (Global Data Area) Data resides in a common location and is shared by all Natural objects in the application
PDA (Parameter Data Area) Data passed between a natural program and its subprogram, subroutine or a help routine.
Local Data Area
Variables defined as local can be used only within a single NATURAL module. There are two options for defining local data: Defining the data within the program. Defining the data in a local data area outside the program.
In the below example, the fields are defined within the DEFINE DATA statement of the program. In the second example, the same fields are defined in a local data area, and the DEFINE DATA statement only contains a reference to that data area.
Internal Use
Introduction to NATURAL
Example 1 - Fields Defined within a DEFINE DATA Statement:
DEFINE DATA LOCAL 01 #EMP-ID 01 #EMP-NAME 01 #EMP-ADDR 01 #EMP-CITY 01 #EMP-SAL END-DEFINE Example 2 - Fields Defined in a local data area outside the program. (N20) (A25) (A20) (A25) (P9.2)
Sample LDA: 01 #INPUT-FILE R 01 #INPUT-FILE 02 #GRP-NUM 02 #EMP-NO 02 #EMP-NAME N 9.0 N 9.0 A 10 A 28
Program reference: DEFINE DATA LOCAL USING SAMPLDA END-DEFINE
Global Data Area
The data elements that are to be used by more than one program, routine should be defined in a global data area. Variables defined in a global data area may be referenced by several objects in an application. The global data area and the objects which reference it must be in the same library. Global data areas must be defined with the data area editor, and a program using that data area must reference it in the
Internal Use
Introduction to NATURAL
DEFINE DATA statement. Any number of main programs, external subroutines and help routines can share the same global data area.
Each object can reference only one global data area; that is, a DEFINE DATA statement must not contain more than one GLOBAL clause. Example: DEFINE DATA GLOBAL USING SAMPLGDA END-DEFINE
Parameter Data Area
Parameter data areas are used by subprograms and external subroutines. A subprogram is invoked with a CALLNAT statement. With the CALLNAT statement, parameters can be passed from the invoking object to the subprogram. These parameters must be defined with a DEFINE DATA PARAMETER statement in the subprogram: they can be defined in the PARAMETER clause of the DEFINE DATA statement itself; or they can be defined in a separate parameter data area, with the DEFINE DATA PARAMETER statement referencing that parameter data area.
The sequence, format and length of the parameters specified with the CALLNAT/ PERFORM statement in the invoking object must exactly match the sequence, format and length of the fields specified in the DEFINE DATA PARAMETER statement of the invoked subprogram/subroutine. However, the names of the variables in the invoking object and the invoked subprogram/subroutine need not be the same (as the parameter data are transferred by address, not by name). Sample PDA: 01 # GRP-DET R 01 # GRP-DET 02 #GRP-NUM 02 #EMP-NO 02 #EMP-NAME Program reference: DEFINE DATA PARAMETER USING SAMPLPDA END-DEFINE N 9.0 N 9.0 A 10 A 28
Internal Use
Introduction to NATURAL
Defining Database Views
View consists of database fields to be referenced. It can be same as defined in DDM May define own subset of DDM May use same database fields in different view May be defined internal or external (using data areas). SYNTAX: View-name VIEW [OF] DDM-name { level {ddm-field [(format length)] } Example: 1 FX-BOOK VIEW OF GTI-FX-BOOK 2 TRAN-TYPE 2 GTD-RECEIPT-IND 2 REDEFINE GTD-RECEIPT-IND 3 #RECEIPT-IND 2 SOURCE-SYSTEM 2 C*CHANGE-HISTORY-GROUP 2 CHANGE-HISTORY-GROUP 3 PREVIOUS-CHANGE-USER 3 PREVIOUS-CHANGE-DATE (1:99) (A1)
Defining User Variables
Variables can be defined and used when required and now that we know the data types the variables can fit in, lets see how to use them. For Example: DEFINE DATA LOCAL 01 #NAME 01 #EMP-ID (A25) (N10)
01 #EMP-SAL (N10.2) END-DEFINE The variables should be declared within the DEFINE DATA and END-DEFINE section. The variables can be used only when declared. NATURAL throws a syntax error if variables are used
Internal Use
Introduction to NATURAL
without declaring them initially. The layouts of input file / output file and other working storage variables can be declared and used. In the above example, the variable #NAME is alphanumeric and of size 25, which means that it can hold names that have 25 characters or less.
Re-defining User Variables
Now that we know how to define variables, and if you observe closely they are not just defined, but defined in levels. Weve seen examples where the variables are declared in 01 level. A variable can be redefined and can have sublevels. Let us take the same example and try redefining the 01 level variable.
DEFINE DATA LOCAL 01 #NAME (A25)
01 REDEFINE #NAME 02 # FORE-NAME (A10) 02 # SURNAME 01 #EMP-ID (N10) (A15)
01 #EMP-SAL (N10.2) END-DEFINE
The name comprises of two parts, forename and surname. The above redefinition shows that the forename is alphanumeric and of size 10 and surname is alphanumeric and of size 15 and both adds up to 25 characters.
NATURAL Statements
Lets have a look at some of the most frequently used NATURAL Statements in detail. ACCEPT/REJECT Syntax: ACCEPT IF logical-condition REJECT IF logical-condition Example: READ EMPLOY-VIEW ACCEPT IF #SEX = 'F' AND #MAR-STAT = 'S' DISPLAY #NAME END-READ
Internal Use
Introduction to NATURAL