ILE C/C++ Programmer's Guide

Example: a User-Defined CL Program that Calls an ILE C++ Program

Figure 246 shows how to retrieve a return value from main. A CL command called SQUARE calls an ILE C++ program SQITF. The program SQITF calls another ILE C++ program called SQ. The program SQ returns a value to program SQITF.

You use the CL command prompt SQUARE to enter the number you want to determine the square of for the ILE C++ program SQITF:

CMD        PROMPT('CALCULATE THE SQUARE')
PARM       KWD(VALUE) TYPE(*INT4) RSTD(*NO) RANGE(1 +
           9999) MIN(1) ALWUNPRT(*YES) PROMPT('Value' 1)
Note:
Returning an integer value from an ILE C++ program may impact performance.

Programming Tasks

  1. To create a CL command prompt SQUARE using the source shown below, type:

    CRTCMD CMD(MYLIB/SQUARE) PGM(MYLIB/SQITF) SRCFILE(MYLIB/QCMDSRC)

    Figure 243. SQUARE -- CL Command Source to Receive Input Data


    
    
    CMD PROMPT('CALCULATE THE SQUARE')
    PARM KWD(VALUE) TYPE(*INT4) RSTD(*NO) RANGE(1 +
    9999) MIN(1) ALWUNPRT(*YES) PROMPT('Value' 1)

    You use the CL command SQUARE to enter the value for the ILE C program SQITF.

  2. To create a program SQIFT using the source shown below, type:

    CRTBNDC PGM(MYLIB/SQIFT) SRCFILE(MYLIB/QCSRC)

    Figure 244. SQITF -- ILE C Source to Pass an Argument by Value


    
    
    /* This program SQITF is called by the command SQUARE. This */
    /* program then calls another ILE C program SQ to perform */
    /* calculations and return a value. */
    #include <stdio.h>
    #include <decimal.h>
    #pragma linkage(SQ, OS) /* Tell compiler this is external call, */
    /* do not pass by value. */
    int SQ(int);
    int main(int argc, char *argv[])
    {
    int *x;
    int result;
    x = (int *) argv[1];
    result = SQ(*x);
    /* Note that although the argument is passed by value, the compiler */
    /* copies the argument to a temporary variable, and the pointer to */
    /* the temporary variable is passed to the called program SQ. */
    printf("The SQUARE of %d is %d\n", *x, result);
    }
  3. To create the program SQ using the source shown below, type:

    CRTBNDC PGM(MYLIB/SQ) SRCFILE(MYLIB/QCSRC) OUTPUT(*PRINT)

    Figure 245. SQ -- ILE C Source to Perform Calculations and Return a Value


    
    
    /* This program is called by another ILE C program called SQITF. */
    /* It performs the square calculations and returns a value to SQITF. */
    #include <stdio.h>
    #include <decimal.h>
    int main(int argc, char *argv[])
    {
    int *vin;
    int vout;
    vin = (int *) argv[1];
    vout = (*vin)*(*vin);
    return(vout);
    }

    The program SQ calculates an integer value and returns the value to the calling program SQITF.

  4. To enter data for the program SQITF, type:

    SQUARE

    and press F4 (Prompt).

  5. Type 10, and press Enter. The output is as follows:
    +--------------------------------------------------------------------------------+
    |  The SQUARE of 10 is 100                                                       |
    |  Press ENTER to end terminal session.                                          |
    +--------------------------------------------------------------------------------+

Source Code

Figure 246. User-Defined CL Command SQUARE that Calculates the Square of a Specified Number


 // This program SQITF is called by the command SQUARE. This
 // program then calls another ILE C++ program SQ to perform
 // calculations and return a value.
 
 #include <iostream.h>
 
 extern "OS" int SQ(int); // Tell compiler this is external call,
                         // do not pass by value.
 
 int main(int argc, char *argv[])
 {
  int  *x;
  int  result;
 
  x = (int *) argv[1];
 
  result = SQ(*x);
 
  // Note that although the argument is passed by value, the compiler
  // copies the argument to a temporary variable, and the pointer to
  // the temporary variable is passed to the called program SQ.
 
  cout <<"The SQUARE of" <<x <<"is" <<result <<endl;
}

The ILE C++ program SQ calculates an integer value and returns
the value to the calling program SQITF:

// This program is called by a ILE C++ program called SQITF.
// It performs the square calculations and returns a value to SQITF.
 
int main(int argc, char *argv[])
 
{ return (*(int *) argv[1]) * (*(int *) argv[1]);
}

Using the CL Command SQUARE to Return the Calculated Value

To enter data for the program SQITF:

  1. Enter the command SQUARE and press F4 (Prompt).
  2. Type 10, and press Enter.

The output is:

+--------------------------------------------------------------------------------+
|  The SQUARE of 10 is 100                                                       |
|  Press ENTER to end terminal session.                                          |
+--------------------------------------------------------------------------------+


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