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:

    Sinclair Lewis is the perfect example of the false sense of time of the newspaper world.... [ellipsis in source] He was always dominated by an artificial time when he wrote Main Street.... He did not create actual human beings at any time. That is what makes it newspaper. Sinclair Lewis is the typical newspaperman and everything he says is newspaper. The difference between a thinker and a newspaperman is that a thinker enters right into things, a newspaperman is superficial.
    Gertrude Stein (1874–1946)

    Institutional psychiatry is a continuation of the Inquisition. All that has really changed is the vocabulary and the social style. The vocabulary conforms to the intellectual expectations of our age: it is a pseudo-medical jargon that parodies the concepts of science. The social style conforms to the political expectations of our age: it is a pseudo-liberal social movement that parodies the ideals of freedom and rationality.
    Thomas Szasz (b. 1920)