As the name subprogram suggests, a subroutine behaves in much the same way as a computer program that is used as one component in a larger program or another subprogram. A subroutine is often coded so that it can be called several times from several places during execution of the main program, including from other subroutines, and then return to the next instruction after the initial call once the subroutine's task is completed.
In computer programming, a subroutine is a sequence of program instructions that perform a specific task and organized into one unit. This unit can then be used in programs wherever that particular task should be performed.
Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs.
In different programming languages, a subroutine may be called a procedure, a function, or method. The name subprogram suggests a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be called several times and from different locations in the main program, including from other subroutines. Later in the execution of the program, it can then branch back to the next instruction after the call, once the subroutine's task is completed. Subroutines are a powerful programming tool that have existed since PDP-11 and the syntax of many programming languages includes support for writing and using them.
The careful use of subroutines (for example, through the structured programming approach) will substantially reduce the cost of developing and maintaining a large program, while increasing its quality and reliability. Subroutines, which are often collected into libraries, are an important mechanism for sharing software. The discipline of object-oriented programming is based on objects and methods (which are a more elegant way of expressing subroutines) attached to these objects or object classes.