No generosity please


Generosity is mostly regarded as a desirable virtue but not with compilers.

Function (mis-)used as a variable

const byte keyPin = 11;

boolean keyPressed(byte pinNum) {
  return digitalRead(pinNum);
}

void setup() {
  pinMode(keyPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (keyPressed) Serial.println("key was pressed");
  delay(100);
}

The code above compiles without errors but the boolean expression in the if-clause always evaluates to true.
Even if you replace the function by

boolean keyPressed(byte pinNum) {
  return false;
}

it keeps on going to print on the terminal line after line.

Missing keyword case

const byte north =  90;
const byte west = 180;
const int south = 270;
const int east = 360;
// any more 4-letter-words?

long long  direction = 0;

void doThis() {}

void doThat() {}

void doSomething() {}

void andThat() {}

void doItAgain() {}

void setup() {
  // nothing to be done
}

void loop() {
  switch (direction += 90) {
    south : doThis(); break;
    north : doThat(); break; andThat(); break;
    west  : doSomething(); break;
    east  : doItAgain(); break;
  }
}

At a first glance it might look quite unsuspicious.
At a second glance you will detect the missing case keywords.
And a java compiler mercilessly would reject the andThat call as it is unreachable code.
But Arduino will compile it without error messages - but none of the functions ever will be called.

P.S.: long long really exists (64 bits) but it will take half of the controller's registers.


contact: nji(at)gmx.de