0

I'm trying to experiment with a "return" function. Yet each time I try to run my program, there happens to be an error, like "expected declaration before '}' token."

Can someone explain the basics of a return function, and look at my code below to possibly see what the problem could be?

{
int index;

pinMode(10, INPUT); //button input

int checkInput();

if (digitalRead(10) == 0) {

return 1;
else{

return 0;
 }
}
  • What is the line int checkInput(); supposed to achieve? – Majenko Mar 25 '16 at 23:38
  • return isn't a function, it's a statement. In case the answers didn't make it clear, you change int checkInput(); to checkInput(); – Nick Gammon Mar 26 '16 at 6:45
3

It has nothing to do with the retun statements. You have to close each { with a }. you are missing one.

   {
int index;

pinMode(10, INPUT); //button input

int checkInput();

if (digitalRead(10) == 0) {

return 1;}
else{

return 0;
 }
}
3

That piece of "code" is, I am afraid to say, complete gibberish.

Passed through Artistic Style to reformat it ends up with:

{
    int index;

    pinMode(10, INPUT); //button input

    int checkInput();

    if (digitalRead(10) == 0) {
        return 1;
        else {
            return 0;
        }
    }

Starting from the top of the program the errors are:

  1. You have a bracket hanging at the front of the snippet for some reason
  2. int checkInput(); is a function prototype (forward declaration) and doesn't do anything other than tell the compiler that you will define this function somewhere else in your sketch.
  3. You are lacking the balancing bracket for the first portion of your if statement, so your else { return 0; } never runs.
  4. There is no balancing bracket for the strange one by itself at the start of the snippet.

In short, I have a feeling you need to learn the basics of C syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.