Hi,
you can do so too, but this will internally create copies of the data you move around. When coming from the Java world, you most probably won't get in touch with the call by reference and call by value patterns.
Call by value means that whenever you assign a value to a different variable or call a function with a given parameter which isn't a handle, this data will be copied. Take this one for example:
string teststring="this is a test";
void testfunc(string text)
{
// we will append some data here to demonstrate the difference
text = text + " in bgt";
alert("text", text);
alert("teststring", teststring);
}
void main()
{
testfunc(teststring);
}
What you will notice is the following:
* the first alert, which prints the text variable, will show "this is a test in bgt", because we appended the " in bgt" part.
* the second alert however will still print "this is a test", because we called by value here, means, the content of teststring got copied to a new variable which then got passed to testfunc(), means we can do whatever changes we like to text within testfunc and the original variable teststring won't know anything about it.
Call by reference however passes only the handle to that variable into testfunc, which results in two things:
* less time consuming function calls, since in-memory copies still are time-consuming operations
* the variable can be modified from within the called function. Lets adopt the above example to make things clear.
string teststring="this is a test";
void testfunc(string@ text)
{
// we will append some data here to demonstrate the difference
text = text + " in bgt";
alert("text", text);
alert("teststring", teststring);
}
void main()
{
// note that you don't need to use the @ sign in the two lines below, bgt will do this for you already
testfunc(@teststring);
testfunc(@teststring);
}
What you will notice here is the following:
* the first call to testfunc() will end up alerting two things, and two times the same thing: this is a test in bgt. Thats because the editing of text ended up editing teststring as well, because we passed in a reference here, not a value.
* the second call to testfunc will also print the same thing: this is a test in bgt in bgt, because we again appended stuff to the text variable which is just another name for teststring.
Conclusion:
call by reference can be performed in a given time, no matter the data you are actually handling, since the operation of receiving a handle to a given variable is performed in constant time, whereas copying large arrays to calling a function by value or copying the array between variables can get pretty time consuming. when copying arrays with objects in them you can also run into the problem of shallow copies, meaning that the array itself is actually a copy, but the objects aren't. This can lead to trouble when you're believing that you can freely edit your objects without editing the objects stored separately, but in fact edit both of them at once. Understanding how handles work can therefore improve your programming skills and also bring some significant performance boosts to the programs you're developing, and since handles (or pointers) aren't just important when developing in bgt, but also in C/C++ and other languages, its recommended to get used to them, even if newer high-level languages usually don't require to know about them, since they will do all the stuff for you in the background.
Hope that helped.
Best REgards.
Hijacker
PS: I didn't test the code snippets above and thus don't guarantee them to be working.