Function Doesn't Function

void setup() {
  ///pinMode(PWMout, OUTPUT);  // sets the pin as output
  val = 3;
  ///PWMperiod = 10000; // PWM changes every 10000 uS.
  Serial.begin(9600);
}

void loop() {
///time1 = micros();
  delay(500);
doOneBeaconCycle;
  Serial.print(val);
  Serial.println();
}

void doOneBeaconCycle(){
  val++;
}

I condensed my problem to the code I posted above. My function "doOneBeaconCycle" is never called. Output is always a string of "3"s. If my function was being called the output should print an increasing series of numbers. I'm assuming I've done something wrong but I don't see it.

several compiler errors which means your program never ran

  • need to define val as int
  • doOneBeaconCycle() was missing "()"
int val = 3;

void setup() {
    Serial.begin(9600);
}


void loop() {
    delay(500);
    doOneBeaconCycle ();
    Serial.println (val);
}


void doOneBeaconCycle(){
    val++;
}

Output

4
5
6
7
8
9
10
11
12

Bingo. No () following doOneBeaconCycle. Thank you.

FYI, Had no compiler errors with the code that didn't work.

this was the output from your posted code

C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In function 'void setup()':
Tst:3:5: error: 'val' was not declared in this scope
     val = 3;
     ^~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In function 'void loop()':
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:12:21: warning: statement is a reference, not call, to function 'doOneBeaconCycle' [-Waddress]
     doOneBeaconCycle;
                     ^
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:12:21: warning: statement has no effect [-Wunused-value]
Tst:13:18: error: 'val' was not declared in this scope
     Serial.print(val);
                  ^~~
C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In function 'void doOneBeaconCycle()':
Tst:19:5: error: 'val' was not declared in this scope
     val++;
     ^~~
exit status 1
'val' was not declared in this scope

And did not post the code you were working with, as @gcjr shows that code did not even com;Ike, let alone malfunction.

And I hope you know by now that just because a program compiles, it says next to nothing about whether it will do anything like you want it to.

If only. :expressionless:

a7

Yeah, I failed to copy the declaration of val and it's initial value of 3. It was there in my code but I didnt copy enough of my code to show that.

doOneBeaconCycle, without the parens, does compile with no errors but it just doesn't execute.

there is a warning

C:\stuff\SW\Arduino\_Others\Tst\Tst.ino:12:21: warning: statement is a reference, not call, to function 'doOneBeaconCycle' [-Waddress]
     doOneBeaconCycle;

under the IDE File->Preferences, set Compiler Warnings: to at least More

Yes it does execute, and does exactly what you wrote. It just doesn't do what you hoped.

For reasons that may some day become apparent to you, referring to a function by using its identifier without parentheses has actual purposes.

This line of code

   doOneNeaconCycle;

is not an error, it is perfectly valid code that… does nothing at all, and that is recognized at some point so no code is generated, and it's as good (or worthless) as never having typed it at all.

That's why warnings are important. The compiler has no clue… C and C++ are languages that trust that you know you want to be shooting yourself in the foot, if that's what you write.

It's the high wire act with no safety net.

Other languages take a great deal more care handholding and actually keeping you from making mistakes which are not mistakes. "Real" programmers need to be able to do stuff that C allows, we rely on compiler warnings and other tools to provide some measure of safety.

And still mess up with the power it provides.

a7

2 Likes

You defined "val" in the setup() function so it's not visible in other functions, search "C++ scope".

I agree that "C++ scope" is worth a google and whatever time you spend, but I do not see val defined in any setup() function.

I'm looking through the tiny window, help me find it. If it is there, I can improve my tiny window chops.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.