Function Dilema
colinhasted
Posts: 44
Hi there,
Just a simple question with regards to functions. I'm just writing a quick Gnome sorting function and I'm passing a structure array to the function to be sorted.
My structure array is called test[20] and my function is defined as gnomesort(mystruct test2[20])
Is it possible to change the function type so that it doesn't sort my original array(test[20]) when I pass it, rather, instead sort only the array within the function scope (because I don't want my original array sorted so that I can return a new index)
In C# for instance you have two options, one to work with the original data, the other to instance it.
Sounds like a bizarre request and, yes, it's easy to work around. But it's always good to do things properly!!
Just a simple question with regards to functions. I'm just writing a quick Gnome sorting function and I'm passing a structure array to the function to be sorted.
My structure array is called test[20] and my function is defined as gnomesort(mystruct test2[20])
Is it possible to change the function type so that it doesn't sort my original array(test[20]) when I pass it, rather, instead sort only the array within the function scope (because I don't want my original array sorted so that I can return a new index)
In C# for instance you have two options, one to work with the original data, the other to instance it.
Sounds like a bizarre request and, yes, it's easy to work around. But it's always good to do things properly!!
0
Comments
I found this out the hard way a few years ago. "Where is variable X changing??" Oh, it is passed to my function where it is called variable Y, which is changing....
gnomesort(mystruct test2[])
As Colzie alluded to, Netlinx passes all parameters by reference. We don?t have the option to pass by value.
Welcome to the forum.
Alas, I'll have to take a copy of the array
Don't be sad. Were you to pass by value, the compiler would have had to do the same thing.
Paul
foo(nBar); // passes by reference (int)
foo(1+nBar-1); // passes by value (int)
foo(sBar); // passes by reference (string)
foo("sBar"); // passes by value (string)
Great summary of tricks! Coincidentally, I was just exploring the pass-by-reference vs pass-by-value for strings. As you have suggested, string expressions contained in double-quotes (which can contain string variables) are evaluated at runtime to form a string result, which is passed by value.
Cheers,
Tom
One thing I will do is put a STACK_VAR in the function that will be manipulated. For instance.
Hope this helps.