Home AMX User Forum AMX General Discussion

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"
}

Comments

  • jjamesjjames Posts: 2,908
    You need a RETURN statement. Do you want nTempNum returned? If so, add this as the last instruction in the function.
    RETURN nTempNum;
    

    (P.S. - Use all caps with the code formating. [ CODE ] & [ / CODE ] - spaces removed)
  • mpullinmpullin Posts: 949
    You realize that if debug is false, your FOR loop won't run?

    Do this:
    if(debug){
         SEND_STRING 0, "'fnAddCkSum()'"; // if you want to comment this line out that is fine
    }
    
    Or this:
    if(debug) SEND_STRING 0, "'fnAddCkSum()'"; // if you want to comment out this entire line that is also fine
    
    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 wrote: »
    You need a RETURN statement. Do you want nTempNum returned? If so, add this as the last instruction in the function.
    RETURN nTempNum;
    

    (P.S. - Use all caps with the code formating. [ CODE ] & [ / CODE ] - spaces removed)

    jjames,

    this runs and works, that is why Im so confused... but i dont understand how it gets that value.
  • mpullin wrote: »
    You realize that if debug is false, your FOR loop won't run?

    Do this:
    if(debug){
         SEND_STRING 0, "'fnAddCkSum()'"; // if you want to comment this line out that is fine
    }
    
    Or this:
    if(debug) SEND_STRING 0, "'fnAddCkSum()'"; // if you want to comment out this entire line that is also fine
    
    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]
    }
    

    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.
  • nTempNum = nTempNum + cString[nCount]    /* Would instead be */    nTempNum += cString[nCount] 
    nTempNum = nTempNum % 256    /* Would instead be */    nTempNum %= 256
    

    I've never tried it.. .but does amx allow you to do the above? like in java, c++, etc.. ie. += *= -= /= %= etc.
  • Ok... so I hate to answer my own question.. but a quick search through the netlinx programming guide:

    "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
  • 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.
    Since you're using a debug variable you don't really need to comment out your IF(debug) statements, you would just set your debug variable to 0 in the start of code, and switch it to 1 during debug sessions either in code or more easily, using the Debug console in Netlinx Studio. This allows you to have debug statements all over the place and turn them on and off from one location rather than having to comment them out. If you prefer to comment out each of your debugs you can skip the "IF(debug) and just use SEND_STRING 0, "sMsg".
    nTempNum += cString[nCount] 
    nTempNum %= 256
    
    The above isn't valid.
  • this runs and works, that is why Im so confused... but i dont understand how it gets that value.

    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 like
    SEND_STRING 0, "'Checksum = ',ITOA(fnCalculateChecksum)"
  • Since you're using a debug variable you don't really need to comment out your IF(debug) statements, you would just set your debug variable to 0 in the start of code, and switch it to 1 during debug sessions either in code or more easily, using the Debug console in Netlinx Studio. This allows you to have debug statements all over the place and turn them on and off from one location rather than having to comment them out. If you prefer to comment out each of your debugs you can skip the "IF(debug) and just use SEND_STRING 0, "sMsg".


    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.
  • 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 like
    SEND_STRING 0, "'Checksum = ',ITOA(fnCalculateChecksum)"

    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.
  • jjamesjjames Posts: 2,908
    I come from a java and c++ background, and code wont compile if missing a return, so its interesting netlinx allows it.
    Don't compare Netlinx with any other language - it breaks a lot of rules and isn't your normal language. ;)
  • ...so i'm coming to learn =/


    It's very frustrating at times.
  • PhreaKPhreaK Posts: 966
    You could always just switch to Duet.
  • PhreaK wrote: »
    You could always just switch to Duet.

    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.
Sign In or Register to comment.