Function not compiling due to unqualified-id (beginner programmer)

Hello All,

I have been trying to create a function in my code that would lookup what channels on an RGB LED need to be turned on or off depending upon the value that is fed into the function. I know there are probably simpler ways to write this code but I am trying to reteach myself some of the basics. In my code when I try to run it I encounter 1 of 2 possible errors. The first of which is the "error: expected unqualified-id before ‘if’, and the other being "error: expected unqualified-id before ‘;’

GetColors(degreesF);
void GetColors(float temp)
{
  if (temp <= 40) 
  {
    digitalWrite(RED_PIN, HIGH);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
  } //end of if
  else if ((temp >40) && (temp <= 50))
  {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, HIGH);
    digitalWrite(BLUE_PIN, LOW); 
  } //end of else if

  else 
  {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
  } //end of else
} //end of function

If i move the main function bracket up after the function callout I get the error about the semi colon, but if I move it one line down below it like I have now, I receive the “if” statement errors. I am hoping that this is just a simple mistake and someone can point it out quickly to me. I have been debugging for hours and I simply can not see the issue.

Please post all of your code so it can be seen how you are using the function and where it is in your program.

For instance, where are you calling GetColors ? Your code snippet makes it appear that you are calling it immediately beore the function definition, which I assume is not how you you are using it, or is it ?

Did you read Read this before posting a programming question ... - Programming Questions - Arduino Forum

3 points:

  1. there is no need to test (temp > 40) as that is guarded by the first test
  2. the blue led is always LOW, typo?
  3. type info missing in prototype?
GetColors(float degreesF); 
void GetColors(float temp)
{
  if (temp <= 40) 
  {
    digitalWrite(RED_PIN, HIGH);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
  } 
  else if  (temp <= 50)  
  {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, HIGH);
    digitalWrite(BLUE_PIN, LOW); 
  } 
  else 
  {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
  } 
}

The first line in your code should not be there.

GetColors(degreesF);

Is that hanging around outside any function block by itself, or did you just "forget" to post the code that that function call is included in?

A function name like GetColors() implies that it is going to return some values. Your function does not RETURN anything, so the Get part is wrong.

Thanks everyone for the feedback.

To clear things up, I am not calling the function inside the function block. I have omitted the rest of the code because I was able to debug it and I isolated the issue to the function declaration. I put the function call out to show how I was using it. The function is reading in a float value (degreesF) which can be a number between -32 and 100. The purpose of the function is to evaluate the number against the conditional statements and turn on the LED channels according to which case the number falls in.
i.e. 90 - 100 Red LED High - Blue LED Low - Green LED Low
80-90 R Red LED Low - Blue LED Low - Green LED High
etc.

I called it GetColors because originally I was having it return a value to me but then I changed the architecture which did not return anything but I went ahead and left the function name as is.

Also I am aware that the blue led is always off in this example because I did not finish writing all of the else if cases. I was trying to get the two conditionals to work before moving on. I am pretty sure the issue is that I have the wrong nomenclature or structure somewhere in the function that I can't find, and I am sure I can not find it because I don't know what I am looking for.
If someone asked you to grab a tool that you have neither seen nor heard of out of a tool box full of more tools you can't identify, then you would spend hours staring at the noel never realizing you are staring right at it.

Hopefully this clears up some of the confusion.

If you get a compiler error message, you should post your entire sketch, not only the part that you think is wrong :slight_smile:

not only the part that you think is wrong

The reason for this. of course, is related to your error message. An unqualified ID occurred BEFORE the part you posted. All we can suggest, based on what you posted, is to remove the problem that lives before the part you posted.