Chapter 6 - Subprogram Control
Chapter 6 - Subprogram Control
Chapter 6 - Subprogram Control
Subprogram Controls
1
Introduction
• Subprograms are the fundamental building blocks of
programs
• it is therefore among the most important concepts in
programming language design.
• A subprogram definition describes the interface to and
the actions of the subprogram abstraction.
• A subprogram call is the explicit request that a specific
subprogram be executed.
• A subprogram is said to be active if, after having been
called, it has begun execution but has not yet completed
that execution.
• The two fundamental kinds of subprograms, are
procedures and functions. 2
Subprogram Sequence control
• Transfers control from one subprogram to
another subprogram (subprogram call)
subA
.
subB
.
.
100 subA();
Return address
101 .
.
101
Run-time stack
3
Subprogram Sequence control
• Subprogram control: interaction among subprograms and
how subprograms manage to pass data among themselves in
a structured and efficient manner.
A C
B
C
A
Run-time stack
4
Subprograms Definition and Activation
• Definition
• The subprogram definition is translated into a template, used to
create an activation each time a subprogram is called.
• Activation: consists of
• A code segment (the invariant part) - executable code and
constants
• An activation record (the dynamic part) - local dataparameters
created anew each time the subprogram is called, destroyed
when the subprogram returns.
• Subprogram execution is implemented by the use of two system-
defined pointers:
• Current-instruction pointer – CIP-address of the next statement
to be executed
• Current-environment pointer – CEP- pointer to the activation
record. 5
Attributes of Data Control
• Names and referencing environments: Two ways to
make a data object available as an operand for an
operation.
• Direct transmission – A data object computed at one
point as the result of an operation may be directly
transmitted to another operation as an operand
• Example: x = y + 2*z;
The result of multiplication is transmitted directly as
an operand of the addition operation.
• Referencing through a named data object – A data
object may be given a name when it is created, and
the name may then be used to designate it as an
operand of an operation.
a=2*z;
x=y+a;
6
Attributes of Data Control
• Program elements that may be named
• Variables
• Formal parameters
• Subprograms
• Defined types
• Defined constants
• Labels
• Exception names
• Primitive operations
• Literal constants
• Names can be simple or composite
• Simple names : identifiers, e.g. Myvar
• Composite names : names for data structure
components, e.g. student[4].last_name
7
Attributes of Data Control
• Associations and Referencing Environments
• Association: binding identifiers to particular data
objects and subprograms
8
Attributes of Data Control
• Associations and Referencing Environments
• Local referencing environment: The set of associations
created on entry to a subprogram that represent formal
parameters, local variables, and subprograms defined only
within that subprogram
• Nonlocal referencing environment: The set of associations
for identifiers that may be used within a subprogram but
that are not created on entry to it.
9
Attributes of Data Control
• Block Structure
• Each program or subprogram is organized as a set of
nested blocks.
• Introduces a new local referencing environment.
Hidden to A
10
Attributes of Data Control
• Block Structure : Example – nested for loops
11
Parameter passing methods
• Parameter-passing methods are the ways in which
parameters are transmitted to and/or from called
subprograms.
• Suppose x is a global variable and we have a function p
that accepts one variable as an argument.
p(x);
• From the implementation point of view, we have two
principal alternatives:
• We may provide p with a copy of x’s value
• We may provide p with x’s address.
• The two most common parameter-passing modes, called
call by value and call by reference, are designed to reflect
the above implementation alternative.
12
Parameter passing methods
• Semantics models of parameter passing
• Formal parameters are characterized by one of three distinct
semantics models:
• They can receive data from the corresponding actual
parameters (in mode)
• They can transmit data to the actual parameters (out
mode)
• They can do both (inout mode)
• A parameter is of in mode type if it allows communication
which is only in the direction from the caller to the function
(the “callee”)
• It is of out mode type if it permits communication only in
direction from the callee to the caller
• Finally, it is inout mode when it permits bidirectional
communication.
13
Parameter passing methods
• The three semantic models of parameter passing
14
Parameter passing methods
15
Parameter passing methods
• Pass-by-value
• The value of the actual parameter is used to initialize
the corresponding formal parameter
• Normally implemented by copying
copy
16
Parameter passing methods
• Pass-by-reference
• Pass an access path
• Also called pass-by-sharing
• Passing process is efficient (no copying and no
duplicated storage)
• Disadvantages
• Slower accesses (compared to pass-by-value) to
formal parameters because of indirect accessing
• Potentials for un-wanted side effects if only on-
way communication is required
• Side effects (functional side effect):- occurs when
a function changes either one of its parameters or
a global variable.
17
Parameter passing methods
• Pass-by-reference: Example with C++
19
Parameter passing methods
• Pass-by-Result: Example with C++
Calling procedure
20
Parameter passing methods
• Pass-by-value-result
• Is an implementation model for inout-mode parameters in
which actual values are copied
• It is the combination of pass-by-value and pass-by-result
• The value of the actual parameter is used to initialize the
corresponding formal parameter, which then acts as a
local variable.
• At subprogram termination, the value of the formal
parameter is transmitted back to the actual parameter.
• Sometimes called call-by-copy because the actual
parameter is copied to the formal parameter at
subprogram entry and then copied back at subprogram
termination.
21
22
23
24
25
26
27
Parameter passing methods
• Pass-by-value-result: Example
Subprogram definition Calling procedure
Result:
a=25
b=36
28
Parameter passing methods
• Pass-by-name
• Is an inout-mode parameter transmission method that
does not correspond to a single implementation model
(by value or by reference)
• When parameters are passed by name, the actual
parameter is textually substituted for the corresponding
formal parameter in all its occurrences in the
subprogram.
• In a call-by-name system, entire formulas are passed as
arguments, to be evaluated within the called function, but
using the symbols defined in the calling program
• Example: Passing a subprograms as a parameter
29