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)
CRTCMD CMD(MYLIB/SQUARE) PGM(MYLIB/SQITF) SRCFILE(MYLIB/QCMDSRC)
Figure 243. SQUARE -- CL Command Source to Receive Input Data
|
You use the CL command SQUARE to enter the value for the ILE C program SQITF.
CRTBNDC PGM(MYLIB/SQIFT) SRCFILE(MYLIB/QCSRC)
Figure 244. SQITF -- ILE C Source to Pass an Argument by Value
|
CRTBNDC PGM(MYLIB/SQ) SRCFILE(MYLIB/QCSRC) OUTPUT(*PRINT)
Figure 245. SQ -- ILE C Source to Perform Calculations and Return a Value
|
The program SQ calculates an integer value and returns the value to the calling program SQITF.
SQUARE
and press F4 (Prompt).
+--------------------------------------------------------------------------------+ | The SQUARE of 10 is 100 | | Press ENTER to end terminal session. | +--------------------------------------------------------------------------------+
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 // 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]);
}
|
To enter data for the program SQITF:
The output is:
+--------------------------------------------------------------------------------+ | The SQUARE of 10 is 100 | | Press ENTER to end terminal session. | +--------------------------------------------------------------------------------+
(C) Copyright IBM Corporation 1992, 2005. All Rights Reserved.