At any rate, one important aspect of a subroutine is that it can be invoked from different places in a program. This is better than copying and pasting a block of code everywhere. The copy-and-paste approach has several problems compared to invoking subroutines.
First of all, if the original code (that we copy from) has a defect, then the defect is multiplied when the code is pasted elsewhere in the program. This means to fix the original code involves locating all the copies, and fixing all those copies, too. It is easy for a programming to miss one or more of these copies! By comparison, the subroutine approach only has one copy of the defective code. Once the definition of the subroutine is fixed, then all the invocations will be using the corrected code.
Secondly, the copy-and-paste approach also create bloat programs. Each pasted copy uses up program space. On the other hand, the subroutine approach only has one copy of the code. The code to invoke a subroutine is, generally speaking, much smaller than the code of a subroutine. Consequently, the use of subroutine helps to make programs smaller.
Copyright © 2006-10-12 by Tak Auyeung