Q:

Function condition being executed without condition being met

I declare my variable
int exceedLimit = 0;

I call a function
    compareLimit(selectChannel,exceedLimit);

This function checks the current value of select channel and if it exceeds the current set limit (high limit is set to 100, and low limit to 50). by default it should not be triggered until the user changes some values.
int compareLimit (int channelID, int isLimitExceeded)
{

if (temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit || temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit)
     isLimitExceeded = 2;      
else
    isLimitExceeded = 1;

return isLimitExceeded;

}

Now here's the problem in the next line of code....In the previous function i returned isLimitExceeded, and then i try to match that returned value to determine if the alarm state should trigger ot not. this if statement is written right after compareLimit
  if (exceedLimit = 2)
             logSubsystem();

When i run this program, my exceedLimit immediately triggers and my logsubsystem function is called...but how can that be since i declared exceedLimit = 0 at the beginning of my cpp file??

A:

if (exceedLimit = 2) should be if (exceedLimit == 2).
if (exceedLimit = 2) Here you're assigning the value 2 to exceedLimit and not comparing its value to 2.
Note that the expression of the assignment returns the assigned value, in this case if(exceedLimit = 2) will be always true, that's why the code below the if-statement is always executed. 

