Sunday, July 28, 2013

Parameter Passing and Assigment

An initialization is an expression that introduces a new name and sets it to some value. For example in C we can write something like this:

int x = 5;

This declares a new variable x and sets the value to 5.

Parameter passing is similar to initialization in the sense that it introduces a new name and gives it a value. The primary difference between parameter passing and initialization is where you write the expression that the name gets initialized with. In an initialization the expression is right there in the initialization; in parameter passing, the expression is the actual parameter at the call site.

Parameter passing can also have variations such as pass-by-var and pass-by-thunk. The same sorts of variations could apply to initialization although these variations are not as widely supported in programming languages. C++ does have a construct that is equivalent to a pass-by-var initialization. In C++, the way that you show pass-by-var parameter passing is by declaring the formal parameter as a reference variable:

int f(int& ref) { ref = 10;}

When you call f, you have to call it with a variable as the actual parameter, and you get pass-by-var:

int x = 5;
// here the value of x is 10

The same rule applies to initialization. You can declare a local variable as a reference. In this case you need to initialize  with a variable and it makes the new name an alias for the name that you use to initialize with, exactly as pass-by-var does:

int x = 5;
int& ref = x;
ref = 10;
// here the value of x is 10

In some languages such as Pascal, when you call a function with no arguments, you just use the function name:

function f begin write(‘hello’); end;

Such function definitions behave similarly to initialize-by-thunk.

Here is another example of unusual initialization: the C preprocessor gives an effect similar to initialize-by-text. A preprocessor directive like

#define f while

lets you write a statement like this:

f(x<0) {...}

and it gets treated just ilke

while (x < 0) {...}

So although there are ways in various languages to get some of the different parameter passing effects in initializations, it is generally ad hoc. What Unobtainabol needs is a generic binding mechanism that includes both parameter passing and initialization in the same framework with the same features. But there are a more issues to deal with before we can suggest such a mechanism. In particular, the name-binding mechanism needs to address logical variables and types as values because these are important features that no perfect language can do without.