as400 interview questions and answers-Part 16
1. What is EXTPGM in RPGLE? Give example of EXTPGM in rpgle.
Ans:
o
EXTPGM
This keyword is used in case of prototyping. So first of all
we will go through the concept of prototyping.
ü Prototyping
·
A prototype tells the
compiler how the parameters of a called program or procedure are defined.
·
Prototyping makes
the compiler to verify that whatever parameters that will be passed to a called
program or procedure appropriately defined.
·
Prototyping benefit
us by showing the error at compile time rather than at run time.
ü Case1:
Program without prototyping
Here, there is
one rpgle program SENDPGM which
calls a CL program ‘MSGSFLCL’ with
parameters MSGID(7), MSGF(10) and MSGOPT(1).
Calling Program
SENDPGM
Columns . . . : 1 71 Browse AMIT/QRPGLESRC
SEU==> SENDPGM
FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
*************** Beginning of data *************************************
* SEND MSG0001 FROM MESSAGE FILE CPF9898 TO PROGRAM MESSAGE QUEUE
C MOVEL 'MSG0001' MSGID
C MOVEL 'AM_MSGF' MSGF
C MOVE 'I' MSGOPT
C EXSR SEND
C SEND BEGSR
C CALL 'MSGSFLCL'
C PARM MSGID 7
C PARM MSGF 10
C PARM MSGOPT 1
C ENDSR
Message ID . . . . . . . . . : MSG0001
Message file . . . . . . . . : AM_MSGF
Library . . . . . . . . . : AMIT
Message text . . . . . . . . : THE ACCOUNT NUMBER CAN NOT BE BLANK
Called program
MSGSFLCL
Columns . . . : 1 71 Browse AMIT/QRPGLESRC
SEU==> MSGSFLCL
FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
*************** Beginning of data *************************************
0001.01 PGM PARM(&MSGID &MSGF &MSGOPT)
0002.00 DCL VAR(&MSGID) TYPE(*CHAR) LEN(7)
0003.00 DCL VAR(&MSGF) TYPE(*CHAR) LEN(10)
0004.00 DCL VAR(&MSGOPT) TYPE(*CHAR) LEN(1)
0006.00
0007.00
0008.00 IF COND(&MSGOPT *EQ 'I') THEN(SNDPGMMSG +
0009.00 MSGID(&MSGID) MSGF(&MSGF))
0010.00
0011.00 IF COND(&MSGOPT *EQ 'C') THEN(RMVMSG PGMQ(*PRV +
0012.00 (*)) CLEAR(*ALL))
0013.00 ENDPGM
****************** End of data ****************************************
When we run the
program SENDPGM then at runtime it checks if the parameters are appropriately
passed or not. If we change PARM “MSGID”
to some other data type or change its length, we will get the error but not at
compile time. We will get the error at run time and our program cannot continue
further.
To make sure
that the program doesn’t crash at run time,
we detect the error at compile time
itself by using prototyping. This is
depicted in Case2 below.
ü Case2:
Program with prototyping
To use prototyping, we do the following modifications in the
calling program SENDPFM.
Here we have changed the Parameter MSGID’s length from 7 TO
3.
Columns . . . : 1 71 Browse AMIT/QRPGLESRC
SEU==> SENDPGM
FMT ** ...+... 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7
DMSGID S 3 >>>> Parm MSGID’s length changed from 7 to 3
DMSGF S 10
DMSGOPT S 1
DMSGSFLCL PR EXTPGM('MSGSFLCL')
DMSGID 7
DMSGF 10
DMSGOPT 1
* SEND MSG0001 FROM MESSAGE FILE CPF9898 TO PROGRAM MESSAGE QUEUE
C MOVEL 'MSG0001' MSGID
C MOVEL 'AM_MSGF' MSGF
C MOVE 'I' MSGOPT
C EXSR SEND
C SEND BEGSR
C CALLP MSGSFLCL(MSGID:MSGF:MSGOPT)
C ENDSR
Now when we compile the program,
we get the below error at compile time.
**Error: The
type and attributes of the parameter do not match those of the prototype.
But
the error that we have got here is at compile time that is what we wanted.
2. How to use EXPORT and IMPORT keyword in rpgle?
Ans:
o EXPORT
& IMPORT
Export indicates that the variable has been defined
(stored) in this module and will be used by some other module which is
importing this variable using Import keyword.
Import indicates that the variable has been defined
(stored) in some other module and will be used here.
Example:
Columns . . . : 6 76 Edit AMIT/QRPGLESRC
SEU==> MODULE1
FMT H HKeywords++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*************** Beginning of data *************************************
0001.00 HOPTION(*NODEBUGIO)
0002.00 DVAR1 S 5 0 EXPORT INZ(11111)
0003.00 DVAR2 S 5 0 IMPORT
0003.03 C CALLB 'MODULE2'
0003.04 C EVAL VAR1=VAR2 +33333 >>>>>>>>>>>>>>> VAR2=55555
0005.00 C VAR1 DSPLY
0006.00 C SETON LR
****************** End of data ****************************************
Columns . . . : 6 76 Edit AMIT/QRPGLESRC
SEU==> MODULE2
FMT D DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
*************** Beginning of data *************************************
0002.00 DVAR1 S 5 0 IMPORT
0003.00 DVAR3 S 5 0 IMPORT
0003.07 DVAR2 S 5 0 EXPORT
0003.08 C CALLB 'MODULE3'
0004.00 C EVAL VAR2=VAR1+VAR3 >>>>>>>>>>>>>>> VAR2=22222
0005.00 C RETURN
****************** End of data ****************************************
Columns . . . : 6 76 Browse AMIT/QRPGLESRC
SEU==> MODULE3
FMT D DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++
*************** Beginning of data *************************************
0002.00 DVAR3 S 5 0 EXPORT
0004.00 C EVAL VAR3=11111 >>>>>>>>>> VAR3=11111
0004.01 C RETURN
****************** End of data ****************************************
CRTPGM PGM(AMIT/PGM1) MODULE(AMIT/MODULE1 AMIT/MODULE2 AMIT/MODULE3)
Below picture depicts the data
flow due to import/export: