Return Integer function, but no return ...?
So, I have a piece of code that I would think should be returning an int, however there is no return... what is it returning or how is it compiling and working?
DEFINE_FUNCTION INTEGER fnAddCkSum(CHAR cString[])
{
STACK_VAR INTEGER nCount
STACK_VAR INTEGER nTempNum
STACK_VAR CHAR cUpper
STACK_VAR CHAR cLower
//if(debug)
//SEND_STRING 0, "'fnAddCkSum()'" // DEBUG
FOR (nCount = 1; nCount <= LENGTH_STRING(cString); nCount++)
{
nTempNum = nTempNum + cString[nCount]
}
nTempNum = nTempNum % 256
cUpper = TYPE_CAST((nTempNum / 16) + 48)
cLower = TYPE_CAST((nTempNum % 16) + 48)
cString = "cString,cUpper,cLower,CR,LF"
}
0
Comments
(P.S. - Use all caps with the code formating. [ CODE ] & [ / CODE ] - spaces removed)
Do this:
if(debug){ SEND_STRING 0, "'fnAddCkSum()'"; // if you want to comment this line out that is fine }Or this: Not this:if(debug) //SEND_STRING 0, "'fnAddCkSum()'" // commenting this out is asking for trouble FOR (nCount = 1; nCount <= LENGTH_STRING(cString); nCount++) // this loop now falls under the conditional { nTempNum = nTempNum + cString[nCount] }jjames,
this runs and works, that is why Im so confused... but i dont understand how it gets that value.
Thanks Matt, yes I understand that.. i just commented that out, because the device is polled every second, and it calls this function. I placed the debug statement in, to follow where things are traveling.
I've never tried it.. .but does amx allow you to do the above? like in java, c++, etc.. ie. += *= -= /= %= etc.
"The return type is optional and can be any intrinsic data type or array of intrinsic types that NetLinx supports except a structure or an array of structures.
The function name must not be previously defined constant or variable or a name assigned to a buffer, a wait, DEFINE_CALL, or Function.
Function names are not case sensitive.
"
So, I take it, there is no function overloading allowed... ?
Thanks for all bearing with me, Im learning more everyday, and coming up with more questions daily as well.
-Paul
The above isn't valid.
The 'RETURN' command assigns the value of nTempNum to fnAddCkSum which is defined as an integer in your function declaration.
In other words, a function can do stuff without returning a value, or it can be used to calculate a number and return it.
Here are some examples:
DEFINE_FUNCTION fnCableBoxPower(INTEGER nState) { IF (nState=1) PULSE[dvCABLEBOX,17] IF (nState=0) PULSE[dvCABLEBOX,96] ELSE SEND_STRING 0, "'Unexpected Argument: fnCableBoxPower expecting 1 for ON or 0 for OFF as parameter'" }You would call it with fnCableBoxPower(nCmd) and it would turn a cable box on or off.
DEFINE_FUNCTION INTEGER fnCalculateChecksum(nAddr,nCmd1,nCmd2,nDat1,nDat2) { nChecksum = (nAddr+nCmd1+nCmd2+nDat1+nDat2)%256 RETURN nChecksum }could be used likeSEND_STRING 0, "'Checksum = ',ITOA(fnCalculateChecksum)"
John,
thanks, I actually have other statements throughout the code, this one inparticular i commented out because the router is polled and calls that method, and it jut spits out its in that function constantly, so i commented it out until I really need to know.
Its unfortunate, we cant use simple things this +=, so we dont have to write out variables multiple times.
Thanks John, I do understand how this works, I just thought it was odd, a "Call" wouldnt be used if no value really needed to be returned.
And like i said before, it compiles and functions properly.. just didnt seem normal.
I come from a java and c++ background, and code wont compile if missing a return, so its interesting netlinx allows it.
It's very frustrating at times.
I've thought about it, but I would need to approach my boss on getting the license, and eitherway I have to use netlinx, so there is no running away from it; I absoluetly want to use duet as well, but from what I gather it is behind where it should be as well.
Plus, thats a whole new can of worms... When we slow down a bit, I plan to take any duet training AMX can offer me, along with other stuff...
If I could just open eclipse (a current release) and install a plugin, that would be the most ideal for all of this.