Saturday, December 11, 2010

Passing parameters to methods

A. Introduction: passing parameters through value. Languages: Java, C# and other
In Java and C# (and other languages) all parameters are transmitted through value, without exception. Let's see what that means:
1. all primitive types are copied
2. for non-primitive types, the reference variables are copied. That means that the objects themselves are not copied.
3. the methods work only with these copies. That means that the initial variables remain unchanged.


Example ( Java ):




import java.util.HashMap;
import java.util.Map;


public class ParamPassingTest {


public static void main (String... args){
// params passing for primitive types
int sum = 1000;
add(sum, 500);
System.out.println(sum);
sum = add(sum, 500);
System.out.println(sum);

// params passing for non-primitive types
Map map = new HashMap();
map.put(10, "Ten");
System.out.println(map);
change(map);
System.out.println(map);
}

private static void change(Map map){
map.put(1, "One");
map.put(2, "Two");
map = new HashMap();
map.put(3, "Three");
System.out.println("Map inside change method: " + map);
}

private static int add(int sum, int addedValue){
sum += addedValue;
return sum;
}
}

The output:


1000
1500
{10=Ten}
Map inside change method: {3=Three}
{1=One, 2=Two, 10=Ten}

Conclusions:
1. The objects themselves are not copied. Only the references to these objects (the reference variables) are copied. The reference variables change only by invoking the new operator; since both references point to the same object, the changes to the object that happen inside the method by invoking some methods on the object are seen also outside the method, right during the method execution.


2. There is some performance penalty: each time we call a method memory is allocated for the copies of the method parameters. However, the footprint is not big. I only want to point out that this is a little penalty when relating to other programming languages, where params are sent through reference (memory address) and no copies are made.




B. Types of parameter passing

Terms:
- formal parameter = the parameter inside the method
- actual parameter = the parameter outside the method (the parameter that is sent when calling the method)
- L-value = (simple definition) the left part of an expression. An expression produces a result and returns a value; ex: "x = x*2" is an expression. It's a method of saying at what address is a variable saved. We use this in order to express what happens to a parameter.

The problem: Inside a method, the value of the formal parameter can be changed. The question is whether AND when the formal parameter is copied to the L-value of the actual parameter => the following types of parameter passing:

1. value (described above): actual parameter is copied into formal parameter; changes to formal parameter are not returned to the actual parameter. Languages: Java, C#.
2. result: the value of the formal parameter is copied to the actual parameter at the return of the method. During the method execution the value of the formal parameter is not found in the actual parameter. Language: CORBA.
3. value result: a combination between value and result. Language: CORBA, the inout parameter.
4. reference: the L-value of the formal parameter is copied to the L-value of the actual parameter => the method can modify the value and address of the actual parameter right during the method's execution. This happens in C for instance when we pass the pointer to a variable to a function.
5. readonly: can use value or reference passing type; the changes of the actual parameter are seen in the formal parameter (for instance in multi-threaded applications, changes outside the method are seen in the formal parameter), but changing the value of the formal parameter is forbidden by the compiler
6. macro and name: not used anymore. I personally don't know too much about them :)