Docsity

Pass-by-Value-Result and Pass-by-Reference: A Comparison of Parameter Passing Techniques, Study Guides, Projects, Research of Design

Toggle sidebar
bg1
10/20/16&
1&
An Example: pass-by-value-result vs.
pass-by-reference
program foo;
var x: int;
procedure p(y: int);
begin
y := y + 1;
y := y * x;
end
begin
x := 2;
p(x);
print(x);
end
N.&Meng,&S.&Arthur& 1&
pass$by$value$result- pass$by$reference-
x- y- x- y-
(entry-to-p)-
(a5er-y:=-y-+-1)-
(at-p’s-return)-
2& 2& 2& 2&
2& 3& 3& 3&
6& 6& 9& 9&
Aliases can be created due to pass-
by-reference
• Given void fun(int &first, int &second),
– Actual parameter collisions
• E.g., fun(total, total) makes first and second to be
aliases
– Array element collisions
• E.g., fun(list[i], list[j]) can cause first and second
to be aliases if i == j
– Collisions between formals and globals
• E.g., int* global;
void main() { … sub(global); … }
void sub(int* param) { … }
• Inside sub, param and global are aliases
N.&Meng,&S.&Arthur& 2&
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Pass-by-Value-Result and Pass-by-Reference: A Comparison of Parameter Passing Techniques and more Study Guides, Projects, Research Design in PDF only on Docsity!

An Example: pass-by-value-result vs.

pass-by-reference

program foo; var x: int; procedure p(y: int); begin y := y + 1; y := y * x; end begin x := 2; p(x); print(x); end N. Meng, S. Arthur 1 pass-­‐by-­‐value-­‐result pass-­‐by-­‐reference x y x y (entry to p) (a5er y:= y + 1) (at p’s return) 2 2 2 2 2 3 3 3 6 6 9 9

Aliases can be created due to pass-

by-reference

  • Given void fun(int &first, int &second),
    • Actual parameter collisions
      • E.g., fun(total, total) makes first and second to be aliases
    • Array element collisions
      • E.g., fun(list[i], list[j]) can cause first and second to be aliases if i == j
    • Collisions between formals and globals
      • E.g., int* global; void main() { … sub(global); … } void sub(int* param) { … }
      • Inside sub, param and global are aliases N. Meng, S. Arthur 2

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 N. Meng, S. Arthur 3

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 N. Meng, S. Arthur 4

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) N. Meng, S. Arthur 8

N. Meng, S. Arthur 9

Design Considerations for Parameter

Passing

  • Efficiency
  • Whether one-way or two-way data transfer is needed N. Meng, S. Arthur 10

Parameters that are subroutines

  • In some situations, subroutine names can be sent as parameters to other subroutines
  • Only the transmission of computation is necessary, which could be done by passing a functional pointer N. Meng, S. Arthur 13

Two complications with subroutine

parameters

  • Are parameters type checked?
    • Early Pascal and FORTRAN 77 do not type check
    • Later versions of Pascal, Modula-2, and FORTRAN 90 do
    • C and C++ do N. Meng, S. Arthur 14

Two complications with subroutine

parameters

  • What referencing environment should be used for executing the passed subroutine? - The environment of the call statement that enacts the passed subroutine( shallow binding ) - The environment of the definition of the subroutine( deep binding ) - The environment of the call statement that passed it as an actual parameter( ad hoc binding ) N. Meng, S. Arthur 15

An Example

function sub1() { var x; function sub2() { alert (x); }; function sub3() { var x; x = 3; sub4(sub2); }; function sub4(subx) { var x; x = 4; subx(); }; x = 1; sub3(); }; N. Meng, S. Arthur 16

  • For shallow binding, the referencing environment of sub2 is sub
  • For deep binding, the referencing environment of sub2 is sub
  • For ad hoc binding, the referencing environment of sub2 is sub

An Example

function Sent() { print(x); }; function Receiver(func) { var x; x = 2; }; function Sender() { var x; x = 1; Receiver(Sent) }; N. Meng, S. Arthur 19

  • In static-scoped languages, Receiver is not always visible to Sent, so deep binding is natural
  • In dynamic-scoped languages, it is natural for Sent to have access to variables in Receiver, so shallow binding is appropriate

Design Issues for Functions

  • Are side effects allowed?
    • Ada requires in-mode parameters, and does not allow any side effect
    • Most languages support two-way parameters, and thus allow functions to cause side effects N. Meng, S. Arthur 20

Design Issues for Functions

  • What types of values can be returned?
    • FORTRAN, Pascal, and Modula-2: only simple types
    • C: any type except functions and arrays
    • Ada: any type (but subroutines are not types)
    • JavaScript: functions can be returned
    • Python, Ruby and functional languages: methods are objects that can be treated as any other object N. Meng, S. Arthur 21

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

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 N. Meng, S. Arthur 25

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

An Example

sub co1() { … resume(co2); … resume(co3); } N. Meng, S. Arthur 27

  • The first time co1 is resumed, its 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)

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