For example a statement to call the subprogram which

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: in the previous section often influence the selection process. OTHER RELATED CONCEPTS Subprogram Suppose we are writing a program for solving a trigonometric problem. In the problem solution, suppose we need to calculate the square root of three numbers at three different places in the program. We could of course write the steps required for the calculation of square root each time we need them in the program. But a much easier way of handling the problem would be to write these steps once and then refer to them each time we need them. Every programming language provides a way to do this by defining subprograms. A subprogram is a program written in such a way that it can be brought into use in other programs and used whenever needed, without rewriting. A subprogram is referred to by different names in different programming languages. For example, other names used to refer to subprograms are subroutines, subprocedures, and functions. The structure of a subprogram is shown in Figure 12.24. It consists of a header and a body. The subprogram header, which is the first line of a subprogram, provides a name for the subprogram and it may optionally specify a list of parameters (also known as arguments). The subprogram name is used by other programs to refer to the subprogram when they want to use the subprogram. The parameters are specific variable names, numeric values, or other data that you want the subprogram to use as it executes the instructions in its body. The list of parameters is optional because there may be subprograms for which you do not need or want to pass any arguments. The subprogram body contains the set of instructions that perform the intended task of the subprogram. Once created, a subprogram can be used by other programs as often as needed. At whatever points in a program the function performed by the subprogram is required, a statement to call the subprogram is inserted by the programmer. Typically, the subprogram call statement contains the name of the subprogram followed by the list of parameter...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online