next up previous
Next: Procedure Linkage Up: Procedures Previous: Procedures

The Role of Procedures

Procedures and functions in high-level languages encourage good programming style through:

Modularity
Allows large programs to be broken into smaller, independent units, which are easier to understand and maintain.
Readability
Allows a programmer to quickly understand the relationships between functional units in a large program without looking at low-level code. Parameters are used to pass information in and out of procedures.
Code Reuse
The same procedure may be called from several places in a program, resulting in a shorter program because only one copy of the procedure is needed.

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.

tabular1548

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.



CS 301 Class Account
Mon Sep 13 11:15:41 ADT 1999