Chapter 35
Introduction to Subprograms
35.1 What Exactly is a Subprogram?
In computer science, a subprogram is a block of statements packaged as a unit that performs a specific task. A subprogram can be called several times within a program, whenever that specific task needs to be performed.
In Java, a built-in method is an example of such a subprogram. Take the well-known Math.abs() method, for example. It consists of a block of statements packaged as a unit under the name “abs”, and it performs a specific task—it returns the absolute value of a number.
If you are wondering what kind of statements might exist inside the method Math.abs(), here is a possible block of statements.
Generally speaking, there are two kinds of subprograms: functions and procedures. The difference between a function and a procedure is that a function returns a result, whereas a procedure doesn't. However, in some computer...