Define_function
SCOTTYP
Posts: 32
Hi, I finished programmer 2 about a year and half ago and only do AMX projects about every third month so becoming proficient has been a slow process. In my class we briefly went over define function but did not discuss it a whole lot or use one in class. I was wondering if anyone had some code handy that I could analyze to get a better feel for what context to use it. Forgive me but I havent been able to find my book from the class since my last move and in netlinx studios the definition is not very detailed. Any help is greatly appreciated.
0
Comments
Here is a very simple example of a function:
Some might say that creating a function for something as simple as pulsing a relay to drop a screen is overkill. I disagree. I can drop the screen with one line of code as needed (fn_screen (SET_PWR_ON)) throughout my program. Now let's say this particular screen only drops while the relay is held closed...I can change my function one time, and no matter how many times in my program I need to drop the screen, they all still work. If you hard code "pulse[dv_RELAY, RELAY_SCREEN_DN]" instead of using a function, you have to search through your code and replace it.
This is just a very simple example, but it makes my point (hopefully!). I rarely control a device directly from a button push, rather I have a button push call a function to control a device (or send the command to a module). Early on I ran into too many times of needing to do "something", and putting it in a button push....only later needing to do that same "something" somewhere else. DO_PUSH is a hack (in most cases, in my opinion) to achieve the same result, creating a function is the more elegant solution.
This can be used in an expression like so: SEND_LEVEL dvTP, 1, scaleVolume(inputLevel)
The principal is the same -- reduce duplicated code, especially when you might need to change that code in the future. Even if you only use it once, it lets you write self-documenting code by putting a name on that particular expression or calculation.
SEND_COMMAND dv_TP[fnGET_PANEL(nZone)], "'@TXT-',GET_LAST(nPresets),',',fnSavePreset(GET_LAST(nPresets))"
Where blue words are system functions and the red ones would be user defined . . . just giving a visual reference.
And Colzie - I completely agree with the modifying once and forget about it idea, rather than searching through code and looking for your PULSE command. Good point!
This post has been very helpful with explanations of functions. What I am having problems with is getting data out of the function, assigining it to a structure, and then using that data to do something. I was trying to do something simple like add two numbers and then do a SEND_LEVEL with the result for practice. The example in the book is not very helpful. This is my function:
I know I have to define a structure and then use the dot operator to start assigning values from the function, but my values never change. I don't use functions very often, but when I do, I usually follow this format:
Doing it that way, a press of TP 1 will change nTest to 4. I guess I am missing a step somewhere while trying to pass and and use data from a structure. Any help with that would be much appreciated. Thanks in advance.
Hopefully this addresses the topics you wanted.
Jeff
I know Jeff didn't test this but at a glance (and I could be wrong), I don't think button PUSH dvTp,1 will work. I seem to remember you can't get an array_length form a defined structure. You could just change the FOR loop max "length_array(uTestArray)" to a number for testing.