Passing string by ref in Function
microchip78
Posts: 25
How do I write a function to pass string by ref in it ...
I string putting parameter variable as char, it giving an error, char[] also giving an error and string as variable type also giving an error.
Can someone advise me?
Thanks in advance ...
I string putting parameter variable as char, it giving an error, char[] also giving an error and string as variable type also giving an error.
Can someone advise me?
Thanks in advance ...
0
Comments
The type is CHAR, the brackets go after the parameter name. All parameters are passed by reference.
Thanks for your response ... This is working fine ...
Consider the following: Am I wrong in my thinking?
Reasonably sure. From the help file for "Parameters subroutines":
Your cubeit example never modifies its parameter (x), so it won't test this fact. I tend to avoid modifying parameters since it makes it hard to see what's happening. The only reason I would use it is for a function that needs to return multiple values, in which case the modified parameters would be designated as "out" parameters. Otherwise the function can just return the single value that it produces.
Please don't write code like this even in jest.
Paul
What about it upsets you so?
y = 27
test = 27
Seems like reference to me.
The only thing that I can figure is that 42**3 is a pretty big number to try to store as an integer which seems to me to be a clever and mildly amusing joke.
I've got to go now -- I'm memorizing multiplication tables in hex.
I ran some tests just to help it soak into my brain:
Code triggered every second.
Note that these are stack vars being passed and being modified outside the scope of code that created them. That is unless at compile time the function is pulled into the code block that calls it??
Result:
I was actually joking, but this breaks a few programming rules and I would hate to see it end up in someone's code by accident as it often does. You shouldn't modify outSquare, outCube but rather treat them as read only and return a stack_var. Plus a power() function is probably more appropriate that will calculate any number raised to any exponent. I realize you were demonstrating how to modify two variables in one function call, but generally speaking if you find yourself in the position of needing to do this, there is likely a flaw in the design somewhere, just like gotos.
Paul
I agree completely, although perhaps I didn't state it strongly enough: this isn't really a good idea. Odds are good that you will NEVER find an appropriate use for this code in AMX programming. If you think you have, try to refactor it into two or more functions instead. A little code duplication is better than something that will confuse other programmers maintaining your code (or yourself in 3 months, which is basically the same thing) However, it is possible, and you may see it in another programmer's code, and you should understand it for the sake of completeness.
Jeremy
This function would fit into some code I have fairly easily, but I am also tossing around the idea of just creating an array of the devs and just returning the index pointer to the array. The reason I am considering the function listed is because I think it would be easier for someone else to make changes to the code and this code is being written for others to use it as well as myself.
Jeff
The code below follows the holy programming proclamations set forth by the gods. In your case you don't need to modify a variable passed by reference. All you have to do is return the correct device. This is how standard functions work in other programming languages. C++ will allow you to pass variables by reference and modify them but it is considered sloppy and likely an indicator of poor design.
If you are troubleshooting a block of code it can get very complicated if you have to jump around and look for functions that may or my not modify your variables.
Jeff
I would question the need for a stack_var, then a function call and then a possibly long switch case depending on the number of devices, when you already have the index from get_last you need to send the code to the device in question while it sits waiting patiently in an array.
Paul
My point was just to show the "proper" way to write functions.
I agree that using a function in Jeff's example is not the best method to accomplish what he wants to do, but here is a version of the function that will work.
But don't use this block of code, it's nasty.
Jeff
You don't need a function like that. If you have the index of the UI and the button that triggered the event then all you need to do is reference arrays to send a command to the device you need.
Paul