Chapter 6 - Subprogram Control

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Chapter 6

Subprogram Controls

 Subprogram Sequence control


 Attributes of Data Control
 Parameter transmission

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.

Sub1 Sub2 Sub3

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 dataparameters
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

• Referencing environment: the set of identifier


associations for a given subprogram.

• Referencing operations during program execution:


determine the particular data object or subprogram
associated with an identifier.

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

for (int i=1; i<10;i++)


{
for(int j=1; j<5; j++)
{
cout<<i*j<<endl;
}
cout<<i<<endl;
cout<<j;
Scope problem
}

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

• Types of parameter passing


• Pass-by-value
• Pass-by-result
• Pass-by-value-result (Pass-by-Copy)
• Pass-by-reference
• Pass-by-name

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

int add(int a, int b); int x= add(5, 6);


• When copies are used, additional storage is required
• Disadvantage
• Storage and copy operations can be costly

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++

Subprogram definition Calling procedure

int fun(int& x) void main()


{ {
x=3*x; int a=5;
retuen x; int b=6;
} int c=a+fun(a);
int d=fun(b)+b;
}
Result:
c=? C=5 + 15
D=18+18
d=?
18
Parameter passing methods
• Pass-by-Result
• Is an implementation model for out-mode parameters
• When parameter is passed by result, no value is
transmitted to the subprogram (to the callee).
• The corresponding formal parameter acts as a local
variable; its value is transmitted to caller’s actual
parameter when control is returned to the caller
• In this model, actual parameters of the caller must be
variables
• As a values are returned by copy, it has disadvantages of
storage and copy operation.

19
Parameter passing methods
• Pass-by-Result: Example with C++
Calling procedure

Subprogram definition void caller()


{
void sub(out int a, out int b) int x;
{ int y;
a=6; sub(x, y);
b=4; }
} Result:
x=6
y=4

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

void square(inout int a, void caller(inout int x,inout


inout int b) int y)
{ {
a=5; square(x, y);
b=6; x=x*x;
} y=y*y;
}

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

You might also like