An Example: Pass-By-Value-Result vs. Pass-By-Reference: - Given Void Fun (Int &first, Int &second)
An Example: Pass-By-Value-Result vs. Pass-By-Reference: - Given Void Fun (Int &first, Int &second)
1
10/20/16
Pass-by-Name
• Implement an inout-mode parameter
transition method
• The body of a function is interpreted at
call time after textually substituting the
actual parameters into the function body
• The evaluation method is similar to C
preprocessor macros
An Example in Algol
procedure double(x);
real x;
begin
x := x * 2;
end;
Therefore, double(C[j]) is interpreted as
C[j] = C[j] * 2
2
10/20/16
Another Example
• Assume k is a global variable,
procedure sub2(x: int; y: int; z: int);
begin
k := 1;
y := x;
k := 5;
z := x;
end;
• How is the function call sub2(k+1, j, i)
interpreted?
Disadvantages of Pass-by-Name
• Very inefficient references
• Too tricky; hard to read and understand
3
10/20/16
Implementing Parameter-Passing
Methods
• Most languages use the runtime stack to
pass parameters
– Pass-by-value
• Values are copied into stack locations
– Pass-by-result
• Values assigned to the actual parameters are placed
in the stack
– Pass-by-value-result
• A combination of pass-by-value and pass-by-result
– Pass-by-reference
• Parameter addresses are put in the stack
N.
Meng,
S.
Arthur
7
An Example
• Function header: void sub (int a, int b,
int c, int d)
– a: pass by value
– b: pass by result
– c: pass by value-result
– d: pass by reference
• Function call: main() calls sub(w, x, y, z)
4
10/20/16
5
10/20/16
6
10/20/16
7
10/20/16
function sub1() {
An Example
var x;
function sub2() { • For shallow binding, the
alert (x);
};
referencing environment
function sub3() { of sub2 is sub4
var x;
x = 3; • For deep binding, the
sub4(sub2);
}; referencing environment
function sub4(subx) { of sub2 is sub1
var x;
x = 4; • For ad hoc binding, the
subx();
}; referencing environment
x = 1;
sub3(); of sub2 is sub3
};
N.
Meng,
S.
Arthur
16
8
10/20/16
• Deep binding?
• Ad hoc binding?
9
10/20/16
An Example
function Sent() { • In static-scoped
print(x); languages, Receiver is not
};
function Receiver(func) {
always visible to Sent, so
var x; deep binding is natural
x = 2;
};
• In dynamic-scoped
function Sender() { languages, it is natural for
var x;
Sent to have access to
x = 1;
Receiver(Sent) variables in Receiver, so
}; shallow binding is
appropriate
N.
Meng,
S.
Arthur
19
10
10/20/16
Overloaded Subroutine
• A subroutine that has the same name as
another subroutine in the same
referencing environment, but its number,
order, or types of parameters must be
different
– E.g., void fun(float);
void fun();
• C++ and Ada have overloaded subroutines
built-in, and users can write their own
overloaded subroutines
N.
Meng,
S.
Arthur
22
11
10/20/16
Generic Subroutine
• A generic or polymorphic subroutine takes
parameters of different types on different
activations
• An example in C++
template<class Type>
Type max(Type first, Type second) {
return first > second ? first: second;
}
int a, b, c;
char d, e, f;
…
c = max(a, b);
N.
Meng,
S.
Arthur
23
f = max(d, e);
Generic Subroutine
• Overloaded subroutines provide a
particular kind of polymorphism called ad
hoc polymorphism
– Overloaded subroutines need not behave
similarly
• Parametric polymorphism is provided by a
subroutine that takes generic parameters
to describe the types of parameters
• Parametric polymorphic subroutines are
often called generic subroutines
N.
Meng,
S.
Arthur
24
12
10/20/16
Coroutine
• A special kind of subroutine. Rather
than the master-slave relationship, the
caller and callee coroutines are on a
more equal basis
• A coroutine is a subroutine that has
multiple entry points, which are
controlled by coroutines themselves
Coroutine
• The first execution of a coroutine begins
at its beginning, but all subsequent
executions often begin at points other
than the beginning
• Therefore, the invocation of a coroutine
is named a resume
• Typically, coroutines repeatedly resume
each other, possibly forever
• Their executions interleave, but do not
overlap
N.
Meng,
S.
Arthur
26
13
10/20/16
sub co1() {
…
An Example resume(co2);
…
• The first time co1 is resumed, its resume(co3);
}
execution begins at the first
statement, and executes down to
resume(co2) (with the statement
included)
• The next time co1 is resumed, its
execution begins at the first
statement after resume(co2)
• The third time co1 is resumed,
its execution begins at the first
statement after resume(co3)
N.
Meng,
S.
Arthur
27
Coroutine
• The interleaved execution sequence is
related to the way multiprogramming
operating systems work
– Although there may be one processor, all of
the executing programs in such a system
appear to run concurrently while sharing
the processor
– This is called quasi-concurrency
• Coroutines provide quasi-concurrent
execution of program units
N.
Meng,
S.
Arthur
28
14
10/20/16
Reference
[1] Robert W. Sebesta, Concepts of
Programming Languages, 8th edition, pg.
383-434
15