Procedures and functions in high-level languages encourage good programming style through:
Compilers generate assembly language code to implement procedures and functions in accordance with the syntax and semantics of the high-level language.
The operating system of the computer runs programs as a set of concurrently executing procedures. The operating system calls a program as a procedure to execute the program and the program returns control to the operating system when it completes.
The following example shows a Pascal procedure which swaps the contents of two variables which are passed as parameters:
procedure Swap(var X, Y: integer); {procedure declaration} var Z: integer; begin Z := X; X := Y; Y := Z; {procedure body} end
The procedure is used in a program to swap the values of the variables A and B as shown below:
. . . Swap(A, B); {procedure call} Return: . {return address} . .
The program which calls a procedure is the calling program or caller. The procedure is the called procedure or callee.
The call to execute the procedure is a branch instruction to the beginning of the procedure. When the procedure is finished executing, a second branch instruction returns to the instruction immediately following the procedure call.