Home AMX User Forum AMX General Discussion

Passing variables by reference

Within netlinx is it possible to pass a variable by reference? If so, how?

Comments

  • a_riot42a_riot42 Posts: 1,624
    PhreaK wrote: »
    Within netlinx is it possible to pass a variable by reference? If so, how?

    From the help file:

    The NetLinx compiler passes all variables by reference. This means that the variable the subroutine operates on is the same variable the caller passed. Any change made to a variable passed as a calling parameter updates the value of the variable from the perspective of the caller. You can take advantage of this pass by reference feature to return an updated value through a calling parameter rather than as the return value.

    Constants, on the other hand, are passed by value. When this happens, a copy of the parameter is delivered to the subroutine. Any change made to the variable representing the constant is lost once the function or subroutine finishes.

    Paul
  • PhreaKPhreaK Posts: 966
    Hmmm... the help file. Who would've thought? It must be time for another coffee.

    Cheers for that.
  • mpullinmpullin Posts: 949
    There are other tricks that allow you to pass a variable by value.
    STACK_VAR CHAR sHaystack[20];
    STACK_VAR CHAR sNeedle[20];
    
    // pass by reference
    sHaystack = 'TRANSPORT=1:PLAY';
    sNeedle = REMOVE_STRING([COLOR=red]sHaystack[/COLOR],'=',1);
    // sHaystack = '1:PLAY'
    // sNeedle = 'TRANSPORT='
    
    // pass by value
    sHaystack = 'TRANSPORT=1:PLAY';
    sNeedle = REMOVE_STRING([COLOR=red]"sHaystack"[/COLOR],'=',1);
    // sHaystack = 'TRANSPORT=1:PLAY'
    // sNeedle = 'TRANSPORT='
    
  • PhreaKPhreaK Posts: 966
    Tops. Being able to pass variables round both ways is definitely going to make life a little easier.
  • AuserAuser Posts: 506
    PhreaK wrote: »
    It must be time for another coffee.

    You mean you haven't got a caffeine drip yet? How do programmers ever expect to get the job done without the right tools. Maybe start out with caffeine tablets to get into it and then move on to the harder options...
  • PhreaKPhreaK Posts: 966
    Ha ha yeah... I'm still having to take twice daily caffeine injections to the eye-balls, hopefully my intravenous will be set up soon, it so much easier to get to work and just 'plug in'.
  • a_riot42a_riot42 Posts: 1,624
    mpullin wrote: »
    There are other tricks that allow you to pass a variable by value.
    STACK_VAR CHAR sHaystack[20];
    STACK_VAR CHAR sNeedle[20];
    
    // pass by reference
    sHaystack = 'TRANSPORT=1:PLAY';
    sNeedle = REMOVE_STRING([COLOR=red]sHaystack[/COLOR],'=',1);
    // sHaystack = '1:PLAY'
    // sNeedle = 'TRANSPORT='
    
    // pass by value
    sHaystack = 'TRANSPORT=1:PLAY';
    sNeedle = REMOVE_STRING([COLOR=red]"sHaystack"[/COLOR],'=',1);
    // sHaystack = 'TRANSPORT=1:PLAY'
    // sNeedle = 'TRANSPORT='
    

    Why would you ever do this? I can't think of any reason why I would ever want to pass a string by value when it can be passed by reference. It just opens up a can of bugs.
    Paul
  • DHawthorneDHawthorne Posts: 4,584
    a_riot42 wrote: »
    Why would you ever do this? I can't think of any reason why I would ever want to pass a string by value when it can be passed by reference. It just opens up a can of bugs.
    Paul

    Sometimes it's easier to bust a string up while parsing it, but you don't want to mess up the original in the process. I generally don't bother with any attempts to pass the value in those cases though, I just make a local copy and work on that instead.
Sign In or Register to comment.