ABSTRACT

Writing a structured program involves writing short programs and hiving off the code to a subroutine wherever possible. A structured program contains one short main program and a number of short subroutines or subprograms. A subroutine is a short program that achieves one or a few functionalities. It receives processing parameters from the calling program and returns the results of processing back to the calling program. A subroutine can be called by either the main program or another subroutine. Subroutines are also referred to as subprograms, functions, and methods. In object-oriented methodology (OOM), we have classes and objects that are akin to subroutines. The variables declared inside the subroutine are released when the execution of the subroutine is finished, freeing the RAM held by it. Of course, we can declare variables as global variables that will outlive the subroutine and are held in RAM until the execution of the main program is completed. This chapter discusses all aspects of writing subroutines, including the best practices and the pitfalls associated with programming subroutines.