Subprograms and Parameter Passing

Parameter passing

EXAMPLE: What is the output if y is transmitted by

  1. by value

  2. by reference

  3. by value-result

    begin
    var y : integer;
        procedure P(x : integer);
        begin
            x := x + 1;
            print(x,y);
        end P;

        y := 1;
        P(y);
        print(y);
    end.

Questions

  1. Why does Java require enclosed objects only refer to final variables in the enclosed object?
  2. 
    	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
    			}
    		});
    	

  3. Describe the difference between the way C and Java pass 2D arrays

More on Subprograms

  1. Other Design Issues
    1. Local vars allocated statically or dynamically
    2. Can a subprogram define another subprogram?
    3. Are actual paramters type-checked to see if they match the formal parameters?
    4. Are side effects allowed in functions?
    5. What types can be returned?
    6. How many values can be returned?
    7. Is overloading allowed?

  2. Generics
    1. Example

      public class Stack<T> ...

      <T> search(<T>[] items) ...

    2. Implementation Methods