Version 6.3.8 for OpenVMS
 —  First Steps  —

Subprograms

You will now expand your program to include a CALLNAT statement that invokes a subprogram. In the subprogram, the employees identified from the main program will be the basis of a FIND request to the VEHICLES file which is also part of the demo database. As a result, your output will contain vehicles information from the subprogram as well as employees information from the main program.

The new subprogram requires the creation of an additional local data area and a parameter data area.

When you have completed the exercises below, your sample application will consist of the following modules:

Application structure

This document contains the following exercises:


Modifying the Local Data Area

You will now add more fields to the local data area that you have previously created. These fields will be used by the subprogram that you will create later.

Start of instruction setTo add more fields to the local data area

  1. Return to your local data area.

    E LDA01
  2. If required, press ESC to switch to edit mode.

  3. Define the following fields below #NAME-END (use the line command I and from the resulting window, choose Data Field):

    Level Name Format Length
    1 #PERS-ID A 8
    1 #MAKE A 20
    1 #MODEL A 20

    When all fields have been added, press ESC.

    The local data area should now look as follows:

                           Press <ESC> to enter command mode                        
     Mem: LDA01    Lib: TUTORIAL Type: LOCAL      Bytes:   376  Line:    5 of:   5  
    C T L  Name of Datafield                F     Length Index/Comment             M
      *    *** Top of Data Area ***                                                 
         1 #NAME-START                      A         20                            
         1 #NAME-END                        A         20                            
         1 #PERS-ID                         A          8                            
         1 #MAKE                            A         20                            
         1 #MODEL                           A         20                            
      *    *** End of Data Area ***                                                 
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
      F 1 HELP     F 2 CHOICE   F 3 QUIT     F 4 SAVE     F 5 STOW     F 6 CHECK    
      F 7 READ     F 8 CLEAR    F 9 MEM TYPE F10 GEN      F11 FLD TYPE F12
  4. Stow the local data area.

Top of page

Creating a Parameter Data Area from an Existing Local Data Area

A parameter data area (PDA) is used to specify the data parameters to be passed between your Natural program and the subprogram that you will create later. The parameter data area will be referenced in the subprogram.

With minor modifications, your local data area can be used to create the parameter data area: you will delete two of the data fields in in the local data area and then save the revised data area as a parameter data area. The original local data area remains intact.

Start of instruction setTo create the parameter data area

  1. In the local data area, delete the fields #NAME-START and #NAME-END using the line command D.

  2. Enter the following in the command line of the data area editor.

    SA PDA01

    The current data area is saved with the new name PDA01. The existing local data area is still shown in the editor.

  3. Load the newly created data area into the editor by entering the following command:

    E PDA01
  4. To change the local data area into a parameter data area, enter the following command:

    SET TYPE A

    where "A" denotes parameter data area.

    The object type changes to "Parameter". This is indicated at the top of the screen. The parameter data area should now look as follows:

    Command:                                                                      
     Mem: PDA01    Lib: TUTORIAL Type: PARAMETER  Bytes:   116  Line:      of:   3
    C T    Comment                                                                
      *    *** Top of Data Area ***                                               
         1 #PERS-ID                         A          8                          
         1 #MAKE                            A         20                          
         1 #MODEL                           A         20                          
      *    *** End of Data Area ***                                               
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
      F 1 HELP     F 2 CHOICE   F 3 QUIT     F 4 SAVE     F 5 STOW     F 6 CHECK  
      F 7 READ     F 8 CLEAR    F 9 MEM TYPE F10 GEN      F11 FLD TYPE F12
  5. Stow the parameter data area.

Top of page

Creating Another Local Data Area Containing a Different View

You will now create a second local data area and import fields from the DDM for the VEHICLES database file.

This local data area will be referenced in the subprogram.

Start of instruction setTo create the local data area

  1. Enter the following command in the command line of the data area editor.

    CLEAR

    The data area editor is now empty.

  2. To change the type of the data area, enter the following in the command line:

    SET TYPE L

    where "L" denotes local data area.

  3. Press ESC to switch to edit mode. Then enter the following line command in the first column of the line indicating the top of the data area.

    V
  4. Enter the following in the resulting window and press ENTER:

    +------------------- View Definition -------------------+
    ¦Name of View:          VEHICLES-VIEW                   ¦
    ¦Name of DDM:           VEHICLES                        ¦
    ¦Comment:                                               ¦
    +-------------------------------------------------------+

    A window appears showing the fields of the specified DDM.

                           Press <ESC> to enter command mode                        
     Mem: empty    Lib: TUTORIAL Type: LOCAL      Bytes:     0  Line:    0 of:   0  
    C T    Comment                                                                  
    V *    *** Top of Data Area ***                                                 
      *    *** End of Data Area ***                                                 
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
                                                                                    
    +------------------------------- DDM: VEHICLES --------------------------------+
    ¦    1  AA  REG-NUM                           A         15  N D                ¦
    ¦    1  AB  CHASSIS-NUM                       B          4  F                  ¦
    ¦    1  AC  PERSONNEL-ID                      A          8    D                ¦
    ¦  G 1  CD  CAR-DETAILS                                                        ¦
    ¦    2  AD  MAKE                              A         20  N D                ¦
    ¦    2  AE  MODEL                             A         20  N                  ¦
    +------------------------------------------------------------------------------+
                                                                                    
                                                                                    
                                                                                    
      F 1 HELP     F 2 CHOICE   F 3 QUIT     F 4 SAVE     F 5 STOW     F 6 CHECK    
      F 7 READ     F 8 CLEAR    F 9 MEM TYPE F10 GEN      F11 FLD TYPE F12
  5. Mark the following fields by entering an "X" in the first column:

    PERSONNEL-ID
    CAR-DETAILS
    MAKE
    MODEL

  6. After you have marked all required fields, press ENTER.

    The local data area should now look as follows (use the arrow keys to scroll to the top of the data area):

                           Press <ESC> to enter command mode                      
     Mem:          Lib: TUTORIAL Type: LOCAL      Bytes:   485  Line:    0 of:   5
    C T    Comment                                                                
      *    *** Top of Data Area ***                                               
      V  1 VEHICLES-VIEW                                 VEHICLES                 
         2 PERSONNEL-ID                     A          8                          
      G  2 CAR-DETAILS                                                            
         3 MAKE                             A         20                          
         3 MODEL                            A         20                          
      *    *** End of Data Area ***                                               
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
                                                                                  
      F 1 HELP     F 2 CHOICE   F 3 QUIT     F 4 SAVE     F 5 STOW     F 6 CHECK  
      F 7 READ     F 8 CLEAR    F 9 MEM TYPE F10 GEN      F11 FLD TYPE F12
  7. Save the new local data area by entering the following in the command line:

    SA LDA02
  8. Stow the new local data area.

Top of page

Creating a Subprogram

You will now create a subprogram that uses a parameter data area and a local data area to retrieve information from the VEHICLES file. The subprogram receives the personnel ID passed by the program PGM01 and uses this ID as the basis for a search of the VEHICLES file.

Start of instruction setTo create the subprogram

  1. In the command line of the data area editor, enter the following command:

    E N

    where "N" denotes subprogram.

    An empty program editor is invoked. The object type has been set to subprogram.

  2. Enter the following:

    DEFINE DATA
      PARAMETER USING PDA01
      LOCAL USING LDA02
    END-DEFINE
    *
    FD1. FIND (1) VEHICLES-VIEW
      WITH PERSONNEL-ID = #PERS-ID
      MOVE MAKE (FD1.) TO #MAKE
      MOVE MODEL (FD1.) TO #MODEL
      ESCAPE BOTTOM
    END-FIND
    *
    END

    This subprogram returns to a given personnel ID the make and model of the employee's company car.

    The FIND statement selects a set of records (here: one record) from the database based on the search criterion #PERS-ID.

    In the field #PERS-ID, the subprogram receives the value of PERSONNEL-ID that has been passed by the program PGM01. The subprogram uses this value as the basis for a search of the VEHICLES file.

  3. Stow the subprogram.

    STOW SPGM01

Top of page

Referencing the Subprogram from Your Program

A subprogram is invoked from the main program using a CALLNAT statement. A subprogram can only be invoked via a CALLNAT statement; it cannot be executed by itself. A subprogram has no access to the global data area used by the invoking object.

Data is passed from the main program to the specified subprogram through a set of parameters that are referenced in the DEFINE DATA PARAMETER statement of the subprogram.

The variables defined in the parameter data area of the subprogram do not have to have the same names as the variables in the CALLNAT statement. Since the parameters are passed by address, it is only necessary that they match in sequence, format, and length.

You will now modify your main program so that it can use the subprogram you have just defined.

Start of instruction setTo use the subprogram in your main program

  1. Return to the program editor by entering the following in the command line.

    E PGM01
  2. Insert the following directly above the DISPLAY statement:

    RESET #MAKE #MODEL
    CALLNAT 'SPGM01' PERSONNEL-ID #MAKE #MODEL

    The RESET statement sets the values of #MAKE and #MODEL to null values.

  3. Delete the line containing the DISPLAY statement and replace it with the following:

    WRITE TITLE
          / '*** PERSONS WITH 20 OR MORE DAYS LEAVE DUE ***'
          / '***     ARE MARKED WITH AN ASTERISK        ***'//
    *
    DISPLAY   1X '//N A M E' NAME
              1X '//DEPT'    DEPT
              1X '/LV/DUE'   LEAVE-DUE
                 ' '         #MARK
              1X '//MAKE'    #MAKE
              1X '//MODEL'   #MODEL

    The text defined with the WRITE TITLE statement will appear at the top of each page in the output. The WRITE TITLE statement overrides the default page title: the information which was previously displayed at the top of each page (page number, date and time) is no longer shown. Each slash (/) causes the subsequent information to be shown in a new line.

    Since the subprogram is now returning additional vehicles information, the columns in the output need to be resized. They receive shorter headers. The column in which the asterisk is to be shown (#MARK), does not receive a header at all. One space will be inserted between the columns (1X). Each slash in the header causes the subsequent information to be shown in a new line of the same column.

    Your program should now look as follows:

    DEFINE DATA
      GLOBAL USING GDA01  
      LOCAL USING LDA01
    END-DEFINE
    *
    RP1. REPEAT
    *
      INPUT USING MAP 'MAP01'
    *
      IF #NAME-START = '.' THEN
        ESCAPE BOTTOM (RP1.)
      END-IF
    *
      IF #NAME-END = ' ' THEN
        MOVE #NAME-START TO #NAME-END
      END-IF
    *
      RD1. READ EMPLOYEES-VIEW BY NAME
        STARTING FROM #NAME-START
        ENDING AT #NAME-END
    *    
        IF LEAVE-DUE >= 20 THEN
          PERFORM MARK-SPECIAL-EMPLOYEES
        ELSE
          RESET #MARK
        END-IF
    *    
        RESET #MAKE #MODEL
        CALLNAT 'SPGM01' PERSONNEL-ID #MAKE #MODEL
    *
        WRITE TITLE
          / '*** PERSONS WITH 20 OR MORE DAYS LEAVE DUE ***'
          / '***     ARE MARKED WITH AN ASTERISK        ***'//         
    *
        DISPLAY   1X '//N A M E' NAME
                  1X '//DEPT'    DEPT
                  1X '/LV/DUE'   LEAVE-DUE
                     ' '         #MARK
                  1X '//MAKE'    #MAKE
                  1X '//MODEL'   #MODEL
    *
      END-READ
    *
      IF *COUNTER (RD1.) = 0 THEN
        REINPUT 'No employees meet your criteria.'
      END-IF
    *
    END-REPEAT
    *
    END
  4. Run the program.

  5. Enter "JONES" as the starting name and press ENTER.

    The resulting list should look similar to the following:

    MORE                                                                        
                                                                                
                    *** PERSONS WITH 20 OR MORE DAYS LEAVE DUE ***              
                    ***     ARE MARKED WITH AN ASTERISK        ***              
                                                                                
                                                                                
                                                                                
                                 LV                                             
           N A M E         DEPT  DUE           MAKE                MODEL        
     -------------------- ------ --- - -------------------- --------------------
                                                                                
     JONES                SALE30  25 * CHRYSLER             IMPERIAL            
     JONES                MGMT10  34 * CHRYSLER             PLYMOUTH            
     JONES                TECH10  11   GENERAL MOTORS       CHEVROLET           
     JONES                MGMT10  18   FORD                 ESCORT              
     JONES                TECH10  21 * GENERAL MOTORS       BUICK               
     JONES                SALE00  30 * GENERAL MOTORS       PONTIAC             
     JONES                SALE20  14   GENERAL MOTORS       OLDSMOBILE          
     JONES                COMP12  26 * DATSUN               SUNNY               
     JONES                TECH02  25 * FORD                 ESCORT 1.3          
    
  6. To return to the program editor, enter EDIT at the MORE prompt.

  7. Stow the program.

You have successfully completed this tutorial.

Top of page