Pages

Wednesday, July 31, 2013

Subroutines in CL

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.

7 comments:

  1. 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?

    Frankly, script interfaces such as Bash and Korn are more flexible. The two advantages of CLP are strong typing for variables and compilation.

    ReplyDelete
    Replies
    1. The variables are global, shared between the 'main body' of the CL program & the subroutine.

      Delete
    2. In fact the variables and files can be defined only at the begining of the program

      Delete
  2. Since 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.

    I'm leaning to the first thing mostly, and a "maybe someday" to finish the second.

    ReplyDelete
  3. 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.

    Thanks to IBM for having inclusions these structures in CL programming.-

    Regards,

    J.R. Finol Luque

    ReplyDelete
  4. Why to use sub-routines if we can call procedures in a CL?

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.