Passing variables by reference
PhreaK
Senior Member
Within netlinx is it possible to pass a variable by reference? If so, how?
Comments
-
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 -
Hmmm... the help file. Who would've thought? It must be time for another coffee.
Cheers for that. -
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='
-
Tops. Being able to pass variables round both ways is definitely going to make life a little easier.
-
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... -
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'.
-
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 -
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.
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums