can monkeyc pass parameters by reference ?

Former Member
Former Member
I'm trying to pass variables by reference. eg

function a(x,y) {
x = 23;
y = 15;
}

function dummy() {
var xx,yy;
a(xx,yy);
}

so that xx and yy will have the values assigned by function a.

In pascal you would use procedure a(var x,y: Integer); and in C you would have void a(int &x,&y); but how is this done in monkeyc ?

I couldnt find anything in the docs that even indicates passing by reference can be done, but I'm hoping it can.

TIA Andrew
  • Former Member
    Former Member over 8 years ago
    No can do... you could

    function a(x) {
    x[0] = 23;
    x[1] = 15;
    }

    function dummy() {
    var xx,yy;
    var tmp = [0, 0];
    a(tmp);
    xx=tmp[0];
    yy=tmp[1];
    }

    or better
    class xyz {
    var xx, yy;

    function initialize(x,y) {
    xx = x;
    yy = y;
    }
    }

    function dummy() {
    var x = new xyz(23,15);
    }

    but honestly the pass by reference thing seems like a bad pattern, not knowing your application though I can't suggest a better way.
  • Former Member
    Former Member over 8 years ago
    Ok, thanks for that. I suspected that may be the case and had the class method in the back of my mind.
  • Former Member
    Former Member over 8 years ago
    If you're just looking for a way to return two values, I have used this technique:
    function a() {
    return [23, 15];
    }

    function dummy() {
    var retVal = a();
    }