IBM has been adding commands to CL that have been operations in other languages for many years. IBM i V5R4 saw the introduction of the Subroutine group of commands. There are five commands in this group, but I only use four:
- SUBR - marks the start of the subroutine.
- ENDSUBR - marks the end of the subroutine.
- RTNSUBR - Return from subroutine.
- CALLSUBR - calls the subroutine given.
I am not going to describe what a subroutine is, as I am assuming that all the readers of this blog have at least a basic knowledge of programming principals.
A subroutine can be coded as simply as:
01 PGM 02 03 CALLSUBR SUBR(WHATEVER) 04 05 /*========================================*/ 06 SUBR SUBR(WHATEVER) 07 08 ENDSUBR 09 /*========================================*/ 10 ENDPGM |
The RTNSUBR can be used like the LEAVESR in RPGLE, to exit the subroutine immediately. It has an optional parameter, RTNVAL, that can be used to return a "return code". When I have used this command it was to leave the subroutine, without a need for the RTNVAL. For example:
01 PGM 02 03 CALLSUBR SUBR(WHATEVER) 04 05 /*========================================*/ 06 SUBR SUBR(WHATEVER) 07 RTNSUBR 08 ENDSUBR 09 /*========================================*/ 10 ENDPGM |
You can learn more about these commands on the IBM web site:
This article was written for IBM i 7.1, and it should work with earlier releases too.
Looks rather like subr in RPG II. The IBM pages are rather cryptic, and don't provide nearly enough info. Is this facility from some grudgingly-tolerated skunkworks? Are CLP variables global, thus always shared with all subroutines? One example thus implies. May new variables be local and private to each subroutine? An example implies that returned values must be constants. Why not use a DTAARA instead; why not call service programs instead?
ReplyDeleteFrankly, script interfaces such as Bash and Korn are more flexible. The two advantages of CLP are strong typing for variables and compilation.
The variables are global, shared between the 'main body' of the CL program & the subroutine.
DeleteIn fact the variables and files can be defined only at the begining of the program
DeleteSince the variables used in the CL subroutines have to be global, making the RTNVAR command logically unnecessary, I suspect the RTNVAR command has the purpose of documenting the purpose of the subroutine, and preparing both the RPG developers and the compiler teams for another possible step toward implementing CL subprocedures with independent variable definitions.
ReplyDeleteI'm leaning to the first thing mostly, and a "maybe someday" to finish the second.
I've used subroutines for some time and are very useful. Also I've used other logical structures included recently as DoFor, DoUntil, DoWhile, Select, and all are very useful.
ReplyDeleteThanks to IBM for having inclusions these structures in CL programming.-
Regards,
J.R. Finol Luque
Why to use sub-routines if we can call procedures in a CL?
ReplyDeleteYes, we can use CL procedures too,
Delete