A subprogram is a sequence of instructions whose execution is invoked from one or more remote locations in a program, with the expectation that when the subprogram execution is complete, execution resumes at the instruction after the one that invoked the subprogram. In high-level languages, subprograms are also called subroutines, procedures, and functions. In object-oriented languages, they are usually called methods or constructors. In most modern high-level languages, subprograms can have parameters, local variables, and returned values.
Subprogram linkage refers to the mechanics of communication between a subprogram invoker (the caller) and the subprogram itself (the callee). The caller code consists of a sequence of instructions that
The instruction sequences shown above are in order of execution time. For the caller, this is usually the same as the ordering of the instructions in the code. For the callee, setting up a return value and branching to the return address corresponds to a C language return statement. As in C, it can be done at several places in the subprogram code.
In a C function with void returned value type, the return statement is often omitted from the code. This relies on the fact that the compiler automatically inserts the return branch. In machine or assembly language, it is essential to have the return branch at the end of the subprogram code.
The return address distinguishes subprograms calls from simple branches. The return address is normally the address of the instruction immediately following the branch instruction in the caller code. It provides the capability of having subprogram calls at various places in the code with execution after the call resuming where it left off.
A subprogram linkage protocol is an agreement between caller code and callee code regarding
The choice of a protocol for a subprogram depends on the overall size of the program and the complexity of subprogram nesting. To some extent, the choice also depends on whether the program is written in assembly language or a high-level language.