Code 101: Pass by reference

This is part 1 of a multi-part post on programming fundamentals. This stuff may seem fairly pedestrian to some folks, but then it's probably not targetted at you.

The mysterious pass by reference thing

And that actually there is no such thing as "pass by reference" but simply that when passing a reference-type (aka. "object" in java, c# or "pointer to object" in c++) that it is actually the reference which is passed by value itself. I.e. a copy of a memory address points to the same piece of memory as the original memory address. This super-simple concept is fundamental to understanding almost everything there is to know about how stack and heap memory works.

The below code examples attempt to show what I mean. This is pseudo-code'ish, and anyone who knows java, c#, c or c++ should be able to follow.

void reset(int x)  
{
    x = 5;
}

int a = 10;  
reset(a);

print ("A is " + a); // Should print "10"  

If you are surprised that "10" is printed above, then you definitely need to research this topic. Basically, a copy of the value contained by the variable 'a' is assigned to the method parameter 'x', which is in it's own right a separate variable. Thus when the value 5 is assigned to the variable 'x', it does not magically get also assigned back to 'a'. Therefore 'a' remains as it was, set to it's original value of '10' when the reset() function returns.

Only once you get the above, can you move onto the difficulty in understanding the "reference" bit.

So now lets make it more complex, introducing the mysterious "reference" by way of an object (as before, lets assume java or c#):

class Dog  
{
    public Dog(int w)
    {
        this.v = w;
    }
    public int v;
}

void reset(Dog x)  
{
    x = new Dog(5);
}

Dog a = new Dog(10);  
reset(a);

print ("A is " + a.v); // Should print "10" still...  

Ok, so now we've introduced a "reference" type to overcome our issue in the previous example, and it may now come as a big surprise that this approach still has not had the desired effect of setting our value in the reset() function. Infact, the problem is EXACTLY the same as our previous one. The variable 'x' is a COPY of the value of the variable 'a', the only difference being that it just so happens to contain a reference to an object. When you assign some other value to 'x', it simply takes on the new value, it does not magically pass this value back onto 'a'.

The only way to make meaningful use of the reference is to look-through to the original object:

void reset(Dog x)  
{
    x.v = 5;
}

If this seems basic and obvious - then good, you get it. If it doesn't, please re-produce this example and play with it until you're comfortable with the concept.

This particular problem is one that I love to explore in interview situations for potential developer roles - and after 3 years of running over 50 such sessions can honestly say that not more than 5-10% of developers "get" this. And it may seem like something quite academic, and not important for real-world programming, and maybe only confined to languages like java, c#, c or c++ - but alas NO - this is fairly fundamental in even understanding stuff like AngularJS data binding in JS.