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;
}
}
2 Answers 2
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;
}
}
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:
- You have a bracket hanging at the front of the snippet for some reason
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.- You are lacking the balancing bracket for the first portion of your
if
statement, so yourelse { return 0; }
never runs. - 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.
Explore related questions
See similar questions with these tags.
int checkInput();
supposed to achieve?return
isn't a function, it's a statement. In case the answers didn't make it clear, you changeint checkInput();
tocheckInput();