Subroutines vs. SubProcedures

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Subroutine calls are faster than subprocedure calls. This is a fact.

Subprocedures are better than subroutines. This is an opinion.

Subroutines are better than subprocedures. This is also an opinion (a foolish opinion in my opinion, but an opinion nonetheless).

If RPG had had subprocedures since its early days, IBM would not have created subroutines. Why? Because subprocedures can do everything subroutines can do, except end the program in which they are running. (Why can't subprocedures end their program? That's a long story I'm not going to get into here.)

In addition, subprocedures have local variables and are self-contained. Think about this: It is often easier to clone an entire program than to clone a subroutine.

Suppose you have an existing subroutine and need similar functionality in another location in the same program or in a completely different program. With subroutines, you have to cut/paste the source and then worry about all the fields as well as the field names themselves. You have to make sure you port not only the subroutine C-specs, but also the fields the subroutine uses. Perhaps you can share some of the fields it uses, but that could cause problems, depending on how it was written.

With a subprocedure, when you need similar functionality in another part of your code or in another program, you call the subprocedure, passing the new values to it via parameters. If the subprocedure does only part of what you need, you write another subprocedure that accepts the parameters you want, does the additional tasks, and calls the original subprocedure.

Subprocedures are self-contained units. Not only can you easily call them without concern for mucking up fields used elsewhere in a program, but they are also remarkably easy to clone. You can simply cut/paste subprocedure source code, and unless the subprocedure is heavily tied to global fields (unlikely), you can replicate it and then modify it to suite your needs.

Subroutines have had their day in programming. Using them today is not "bad" like using indicators is bad or using B (binary) fields is bad. But using them should be a last resort. Subprocedures, not subroutines, should be your first choice when programming.

Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.

BOB COZZI

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.


MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: