Subroutine - Main Concepts

Main Concepts

The content of a subroutine is its body, the piece of program code that is executed when the subroutine is called or invoked.

A subroutine may be written so that it expects to obtain one or more data values from the calling program (its parameters or arguments). It may also return a computed value to its caller (its return value), or provide various result values or out(put) parameters. Indeed, a common use of subroutines is to implement mathematical functions, in which the purpose of the subroutine is purely to compute one or more results which values are entirely determined by the parameters passed to the subroutine. (Examples might include computing the logarithm of a number or the determinant of a matrix.)

However, a subroutine call may also have side effects, such as modifying data structures in a computer memory, reading from or writing to a peripheral device, creating a file, halting the program or the machine, or even delaying the program's execution for a specified time. A subprogram with side effects may return different results each time it is called, even if it is called with the same arguments. An example is a random number function, available in many languages, that returns a different random-looking number each time it is called. The widespread use of subroutines with side effects is a characteristic of imperative programming languages.

A subroutine can be coded so that it may call itself recursively, at one or more places, to perform its task. This method allows direct implementation of functions defined by mathematical induction and recursive divide and conquer algorithms.

A subroutine which purpose is to compute one boolean-valued function (that is, to answer a yes/no question) is called a predicate. In logic programming languages, often all subroutines are called predicates, since they primarily determine success or failure. For example, any type of function is a subroutine but not main.

Read more about this topic:  Subroutine

Famous quotes containing the words main and/or concepts:

    It is indeed very possible, that the Persons we laugh at may in the main of their Characters be much wiser Men than our selves; but if they would have us laugh at them, they must fall short of us in those Respects which stir up this Passion.
    Joseph Addison (1672–1719)

    Science is a dynamic undertaking directed to lowering the degree of the empiricism involved in solving problems; or, if you prefer, science is a process of fabricating a web of interconnected concepts and conceptual schemes arising from experiments and observations and fruitful of further experiments and observations.
    James Conant (1893–1978)