CSc 520
Principles of Programming Languages
:

Christian Collberg

Department of Computer Science

University of Arizona

1 Parameter Passing - Value Parameters

1mm
 

1#1
 
2#2

2 Parameter Passing - Reference Parameters

1mm
 

3#3
 
4#4

3 Call-by-Value Parameters

  1. The caller computes the arguments' r-value.
  2. The caller places the r-values in the callee's activation record.


5#5

4 Call-by-Reference Parameters

  1. The caller computes the arguments' l-value.
  2. Expression actuals (like 6#6) are stored in a new location.
  3. The caller places the l-values in the callee's activation record.


7#7

5 Call-by-Name Parameters

Algorithm:

  1. The caller passes a thunk, a function which computes the argument's l-value and/or r-value, to the callee.
  2. The caller also passes a static link to its environment.
  3. Every time the callee references the name parameter, the thunk is called to evaluate it. The static link is passed to the thunk.

6 Call-by-Name Parameters...

Algorithm:

  1. If the parameter is used as an l-value, the thunk should return an l-value, otherwise an r-value.
  2. If the parameter is used as an l-value, but the actual parameter has no l-value (it's a constant), the thunk should produce an error.

Consequences:

7 Call-by-Name Parameters...


8#8

8 Call-by-Name - Implementation


10#10

9 Call-by-Name - Jensen's Device


11#11

10 Large Value Parameters


12#12

11 Large Value Parameters...

 Algorithm 1: Callee Copy Algorithm 2: Caller Copy
 

13#13

14#14

12 Parameter Passing

13 Parameter Passing...

14 Parameter Passing...

In Pascal and Modula-2 a programmer would use call-by-value to

In Pascal and Modula-2 a programmer would use call-by-reference to

15 Parameter Passing...

Modula-3 provides a READONLY parameter mode. A READONLY formal parameter cannot be changed by the callee. The formal

  1. cannot be on the left-hand-side of an assignment statement, and
  2. cannot be passed by reference to another routine.

16 Parameter Passing in Ada

Ada has three modes:

  1. in-parameters pass information from the caller to the callee. The callee cannot write to them.
  2. out-parameters pass information to the callee from the caller. The callee can read and write them. They start out being uninitialized.
  3. in out-parameters pass information from the caller to the callee and back.

17 Parameter Passing in Ada...

For scalars and pointers, all modes should be implemented by copying values. Thus

  1. in-parameters are passed-by-value.
  2. out-parameters are passed-by-result (the formal is copied into the actual when the procedure returns).
  3. in out-parameters are passed-by-value/result (On entry, the actual is copied into the formal. On return, the formal is copied back into the actual).

18 Parameter Passing in Ada...

For constructed types (records, arrays) an implementation is allowed to pass either values or addresses.

19 Parameter Passing in Ada...


15#15

20 Exam Problem 415.330/96 (A)

21 Exam Problem 415.330/96 (B)

17#17

22 Homework


19#19

23 Readings and References

24 Summary



Christian S. Collberg
2005-04-22