Before creating a subroutine, determine if the subroutine contains enough lines and is accessed enough times through one pass of the program to be of any benefit. There are times that straight-line program code will be the optimal solution.

A single line of program code placed in a subroutine will never be executed enough times to be of any benefit. In this case, you would create more program code for a subroutine than you would by using straight-line program code.

Conversely, a subroutine containing four lines of program code becomes beneficial when it is called more than once.

Table 1-10.  Efficient Use of Subroutines.

 

Number of Program Lines

Number of Calls per Program Pass

1 Line

2 Lines

3 Lines

4 Lines or more

1 Call

No

No

No

No

2 Calls

No

No

Even

Yes

3 Calls

No

No

Yes

Yes

4 Calls or more

No

Yes

Yes

Yes

Use the following steps and Table 1-10 to determine when it becomes beneficial to use a subroutine:

  1. Determine how many program lines a subroutine will contain (excluding the RETURN command).
  1. Determine how many times the subroutine will be called through one pass of the program.
  • If the result is "NO," then it is more efficient to use straight-line program code.
  • If the result is "EVEN," then there is no benefit for using either method.
  • If the result is "YES," then it is more efficient to use a subroutine.