Home AMX User Forum NetLinx Studio

Function Dilema

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!!

Comments

  • ColzieColzie Posts: 470
    As far as I know, functions only work with the original data. Obviously the workaround is to make a copy of the data within your function.

    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....
  • Joe HebertJoe Hebert Posts: 2,159
    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])
    Just as an FYI, you can leave out the size to make it more generic and define the function like this if you want:

    gnomesort(mystruct test2[])
    Is it possible to change the function type so that it doesn't sort my original array(test[20]) when I pass it?

    In C# for instance you have two options, one to work with the original data, the other to instance it.
    As Colzie alluded to, Netlinx passes all parameters by reference. We don?t have the option to pass by value.

    Welcome to the forum.
  • Thank you, just out of interest I've been wading through the netlinx help. For functions very little is mentioned. Under DEFINE_CALL however, it mentions that all variables are passed by reference and there is no mentioned way to force by object. Unless of course you are passing a constant!

    Alas, I'll have to take a copy of the array
  • a_riot42a_riot42 Posts: 1,624
    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
  • mpullinmpullin Posts: 949
    As I think there was another thread on this subject, there are "tricks" for passing a parameter by value for strings and integers, but I think in your case, passing a custom type, you'll just have to make the copy yourself.

    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)
  • mpullin wrote: »
    As I think there was another thread on this subject, there are "tricks" for passing a parameter by value for strings and integers, but I think in your case, passing a custom type, you'll just have to make the copy yourself.

    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
  • Another Idea

    One thing I will do is put a STACK_VAR in the function that will be manipulated. For instance.
    DEFINE_FUNCTION fnSomeFunction (MyStruct stInputStruct[])
    {
      STACK_VAR MyStruct stThisStruct[64]
    
      stThisStruct = stInputStruct
    
      //Code below modifys stThisStruct not stInputStruct
    
    
    }
    

    Hope this helps.
Sign In or Register to comment.