printf("%d and %d results in %8.3f\n",a, b, c);
an actual parameter specifies an operand for a subprogram BUT if X is the actual parameter, what do we mean by X. The value of X? The address of X? or the actual name X?
The actual parameter (X) is evaluated at the point of call and this value is transmitted to the subprogram. Assignment to formal parameter does not affect the actual parameter.
Example:
subprogram sub(y) begin y := 9; end; x := 3; call sub(x) call sub(3*x - 2);
IMPLEMENTATION
pointer is transmitted, usually a pointer to the data location containing the value. The formal parameter is then initialized to contain a pointer to the actual parameter. When the formal parameter is used the pointer dereferencing is done either explicitly (C) or implicitly (Java)
Example:
subprogram sub(y) begin y := 9; end; x := 3; call sub(x) call sub(3*x - 2);
IMPLEMENTATION
Example:
subprogram swap(x,y) swap(b[i],i); begin temp := y; temp := i; y := x; i := b[i]; x := temp; b[i] := temp; end; if i = 3 to start and b[3] = 4 and b[4] = 9 then temp gets 3 i gets b[3] which is 4 b[i] which is b[4] gets 3so after the call i = 4, b[3] is unchanged and b[4] is changed to 3.
IMPLEMENTATION
Example: call sub(1,2,1)
- all the same; we want to transmit a pointer/environment pair
- usually the environment at the time the subprogram was called.
- The element is retrieved and its value transmitted as the initial value for the formal parameter.
- The location of the element is retrieved and a pointer to it is transmitted the initial value for the formal parameter.
- Same as by reference.
- The element is retrieved and its value transmitted as the initial value for the formal parameter.
- The location of the element is retrieved and a pointer to it is transmitted the initial value for the formal parameter.
- A thunk is compiled to evaluate the subscript expression in the calling environment and returns a pointer to the appropriate location. Assignment to the formal parameter is allowed BUT the thunk must be evaluated and assignment made to the location returned as its value.
- The data structure is copied (an array for example) and a pointer to the structure is transmitted. (ALGOL 60)
- OR just a pointer to the structure is passed.
- the trade off is protection against modification vs time for copying.
- a pointer to the data structure is transmitted. The formal parameter must be properly subscripted (augmented).
- Can be treated as 'by reference'.
- The referencing environment could be found by traversing dynamic links. BUT this is not natural - It is more likely the programmer would want non-local references to be looked for in the environment of the program which passes the subprogram argument.
P Q R(B) x := 3 use x x := 5 R(Q) B <-- call of QIt is more likely Q would want the x from P and not from R.
If R is a generator function which accepts an array and a function and applies the function to each element of the array. The internal of R might be completely unknown to the user who expects Q to get its value for x from P's environment. In this instance we would prefer to pass an environment for the subprogram to run in as well as the subprogram itself.
- For static block structure we pass an environment pointer to be used as the static link when the subprogram is called.
begin var y : integer; procedure P(x : integer); begin x := x + 1; print(x,y); end P; y := 1; P(y); print(y); end.
public void setup() { int value; value = computeValue(); JButton go = new JButton("GO"); go.addAcionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { theTextField.setText(""+value); //<----------- error here } });
public class Stack<T> ...
<T> search(<T>[] items) ...