ID:813365
 
(See the best response by Forum_account.)
Does BYOND have any type of reference paramater, like an equivalent to an ampersand in C++? For example:

//In C++ reference parameters would function as so
#include <iostream>
using namespace std;

void changeVariable(int &, int &);

int main()
{
int myFirstVariable = 0;
int mySecondVariable = 13;
cout << "Before using changeVariable, myFirstVariable is: " << myFirstVariable << " and mySecondVariable is: " << mySecondVariable << endl;
changeVariable(myFirstVariable, mySecondVariable)
cout << "After using changeVariable, myFirstVariable is: " << myFirstVariable << " and mySecondVariable is: " << mySecondVariable <<endl;
return 0;
}

void changeVariable(int & varOne, int & varTwo)
{
varOne *= 2;
varTwo = varOne + 13;
}


Basically, reference parameters are very useful for changing more than one variable in one function, or even just referencing a variable instead of having to declare it every time it's changed. Any ideas?
Best response
Not that I'm aware of.

When you pass an object or list to a function it's not making a new copy of it, so any changes you make are being made to the instance in the parent proc:

proc
some_proc()
var/list/l = list(1, 2)

// outputs "1, 2"
world << "[l[1]], [l[2]]"

other_proc(l)

// outputs "1, 3"
world << "[l[1]], [l[2]]"

other_proc(list/l)
l[2] = 3
All non-primitives are pass by reference by nature on DM, including list types. Pass by reference for primitives is something of an odd paradigm for what is an OO language, so you find DM elects to follow the route of Java and not provide an option as such to pass them be reference, beyond wrapping them inside a non-primitive object.
In response to Stephen001
Technically object references are passed by value, the value just happens to be a reference to an object. If you passed a reference by reference, this snippet would give you a "cannot read null.name error":

proc
some_proc()
var/mob/m = new /mob()
other_proc(m)
world << m.name

other_proc(mob/m)
m = null

other_proc receives the object reference as a value and has its own copy of that reference. Changing the referenced object will change the object in other places that have references to it, but changing the object reference itself won't affect other object references.
I suspect I've been confusing in my wording, because I already know what you've said.

I suppose the clarification would be that you don't pass non-primitives directly / by value ever as you don't ever have direct / by value access to them, you pass references to the objects around. This is akin to C++'s handling of references, and creating a new object/list etc provides you a reference to that, which you pass around. You can't for example alter or even access a reference itself in some manner in C++, only the object it refers to.