June 2007 Technical Tip Passing pointers to a function in PL/I

I was recently asked by a PL/I programmer how to pass a pointer to a function rather than pass a structure to a function. This article will demonstrate how this is done. First, let's look at the main program.

The structure in this example (VARS) contains three variables (A, B and C). The ALLOCATE statement is used to allocate sufficient storage to hold the structure (a total of six bytes since each of A, B and C is a two-byte packed decimal field. Recall a three digit packed decimal number will occupy two bytes.) The BASED attribute on the declaration of VARS indicates that the declaration itself does not take up any space: think of VARS as a template that describes the six bytes obtained by the ALLOCATE statement. PTR is declared as a pointer (also known as an "implicit locator"), and its use in the BASED attribute indicates that PTR points to the allocated six bytes. MYFUNC is declared as a function which accepts a pointer. Values are assigned to A and B and MYFUNC is called using PTR.


 MYMAIN: PROC OPTIONS(MAIN) REORDER;

    /* DEMO PASSING A POINTER TO A FUNCTION.   */
    /* FUNCTION WILL ADD A AND B GIVING C.     */

    DCL  1  VARS  BASED(PTR) ,
         2  A  FIXED DEC(3,0),
         2  B  FIXED DEC(3,0),
         2  C  FIXED DEC(3,0);

    DCL  PTR       POINTER;
    ALLOCATE VARS;
    DCL  MYFUNC    ENTRY( POINTER );
    DCL  SYSPRINT  FILE;

    A = 2;
    B = 3;
    CALL MYFUNC(PTR);
    PUT SKIP LIST(A, B, C);
    END; /* MYMAIN */

Now let's take a look at the function. The PROC statement in module MYFUNC indicates that it accepts PTR, which is declared as a pointer. VARS is the same structure shown in MYMAIN above. Again, the BASED attribute on the declaration of VARS indicates that the declaration itself does not take up any space. Instead, it is a template which overlays the same six bytes allocated in MYMAIN!


 MYFUNC: PROC(PTR) REORDER;

    DCL  1  VARS  BASED(PTR) ,
         2  A  FIXED DEC(3,0),
         2  B  FIXED DEC(3,0),
         2  C  FIXED DEC(3,0);

    DCL  PTR       POINTER;

    C = A + B;
    END; /* MYFUNC */

The mainframe is alive and well! PL/I, COBOL, Assembler, CICS or JCL: we hope you will consider Caliber Data Training when deciding upon a mainframe training provider.


Go to the articles index. Written by Bill Qualls. Copyright © 2007 by Caliber Data Training 800.938.1222