Subroutines vs. SubProcedures

  • 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.