You are on page 1of 3

Procedure Definitions

• Procedures support control abstraction in programming languages. In


most programming languages, a procedure is defined by two elements:
– A specification (protocol, interface, declaration)
– A body (definition)
• The specification usually identifies the parameters and usually declares
their types.
• In some cases it is critical to be able to separate a procedure’s
declaration from its invocation. This happens, for example, when one
must construct mutually recursive procedures.
• The body usually defines a block of code that is to be executed when
the procedure is called (invoked, activated, executed).
• A procedure is called by specifying its name and a list of arguments
(actual parameters) to be bound to the (formal) parameters of the
procedure. The procedure body is then executed.
• In the old-old days, functions returned values and procedures didn’t. It
is common nowadays to blur the line of distinction between these and
let any procedure return a value.
• Control returns from a procedure when its execution is completed
either by leavingits block or by executing a return statement. The
value returned can either be implicit (the value returned by the last
executed statement in the block) or explicit (the value associate with
the executed return statement.

Procedure Semantics
• Most current language implementations follow the tenets of block
structure in procedure call semantics.
• Each procedure definition is a block.
• When a procedure is invoked, an activation record (frame) is created to
contain name bindings for that procedure during its lifetime. This
activation record contains two critical references:
– The dynamic chain pointer (this points to the caller’s activation
record in the stack. This is remembered so that the caller’s
activation can be restored upon return)
– The static chain pointer (this points to the most recent activation
record of the block in which this procedure was declared. This is
remembered so that references to non-local variables can be
found from the lexical (or static) nesting structure of the
program.
Parameter Passing Methods
• Pass by Value
This method is common and useful. The values of the arguments are
copied into the activation record of the called procedure. As C
demonstrates, aliases can occur if pointers are passed by value.
means passing a copy of the value as an argument.
If a variable is passed, the method receives a copy of the variable's
value. The value of the original variable cannot be changed within the
method. This seems reasonable because the method only has a copy
of the value; it does not have access to the original variable. This
process is called pass by value.

• Pass by Reference

This method allocates a hidden or buried pointer to the argument in


the called procedure’s activation record. An implicit dereference is
performed each time the parameter is used. This allows the call to be
made without copying the argument (which could be a large structure
or array).
Problems can occur if expressions are allowed to be passed by
reference.
means the passing the address itself rather than passing the value

• Pass by Value-Result

This method is quite rare. It involves pass by value (copy-in), followed


by execution of the body, followed by a copy-out of the parameter
values to the original arguments.
Passing Mechanism

1. The values of the arguments are copied into the fomal parameters.
2. The final values of the parameters are copied back out to the
arguments.

Characteristics of the Pass by Value-Result


-AKA copy-in , copy-out(copy-restore)
- no alias problem occurs(differ to the pass by reference in this point).

– The order of copying the results may be important.

• Pass by Name
This method (from Algol 60) was created when parameter passing was
poorly understood. It involves implementing the same behavior as
would occur if the arguments were textually substituted (with
appropriate renamings) in place of the parameters in the body, and the
body were then evaluated. The concept of a thunk(a piece of code to
evaluate the name parameter) was invented to implement this
complex method. The complexity is clear when one realizes it’s
impossible to write a swap procedure using name parameter passing.