Do global variables not have to be defined before the loop?

Hey there!
Trying to self-teach myself arduino, and I'm going through this book (Arduino Cookbook - By O'reilly)
I have a basic intro-background to programming so I know just a little bit.

When I go through, I feel that I have to know why something works or it's hard for me to move on.


First Question:
I was confused when I saw a bool being defined AFTER the loop that calls for the variable.

  • How does it ever call the variable if it isn't defined until after the loop, which is endless?

  • Can global variables be defined wherever outside of setup and loop (before/after/between) and it's all the same?

Here in the example code, see that bool almostEqual is defined outside of and after the void loop.


Secondary question: I actually don't understand how the code for the the almostEqual works, and the book says nothing of it.
I get that we're trying to make a true/false statement

  • Why are there 2 variables inside of 'bool almostEqual(float a, float b)' ?

  • Is "a" supposed to be true, and "b" supposed to be false?

  • Are both "float a" and "float b" being defined right here?

  • For the two ifs "if (a==0) and if (b==0),

  • When were either of these ever assigned a value? Was it in the if statement? Or are variables assigned the value 0 if you never give them a value?


Any explanation would be of great help and thanks so much for sharing some of your time with me!

/*
 * Floating-point example
 * This sketch initialized a float value to 1.1
 * It repeatedly reduces the value by 0.1 until the value is 0
 */

float value = 1.1;

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

}

void loop() {
  value = value - 0.1;
  if (value == 0)

  {
    Serial.println("The value is exactly zero");
  }
  else if (almostEqual(value, 0))
  {
    Serial.print("The value ");
    Serial.print(value, 7);
    Serial.println(" is almost equal to zero, restarting countdown");
    value = 1.1;
  }
  else
  {
    Serial.println(value);
  }
  delay(250);
}
bool almostEqual(float a, float b)
{
  const float DELTA = .00001;
  if (a == 0) return fabs(b) <= DELTA;
  if (b == 0) return fabs(a) <= DELTA;
  return fabs((a - b) / max(fabs(a), fabs(b))) <= DELTA;
}

Normally, you could not define any function before it's referenced. The standard solution is to define it with a "function prototype" and provide the code body "function implementation" further on in the code. Arduino core fudges the standard model so you don't have to do that. It's an attempt to make life simpler for beginners. But it won't work for a variable declaration.

I hope you understand that 'almostEqual' isn't a variable. 'a' and 'b' are exactly what they are defined as, float type.

The values of 'a' and 'b' are passed by the calling code, here it's 'almostEqual(value, 0)'.

almostEqual is a function, not a variable . . . but maybe I read your first question wrong.
It says 'bool' before the actual name of the function "almostEqual" because the almostEqual function will return a boolean value. true, false, 0 (false),
or non-zero integer (true). I apologize if you already understood that.

To answer the original question: Yes, you can declare global variables after loop() but you can't use that variable inside loop() or in any other function above it in the file. You can only use that global variable in functions declared below it in the file. For example, this is a valid sketch:

void loop() {}

int GlobalInt = 3;

void setup()
{
  Serial.begin(115200);
  Serial.println(GlobalInt);
}

Yes. Good thing no genius decided to change this behaviour in the Arduino preprocessor to make things "easier".

int thisVariable;

void setup() {
  	thisVariable = 1;
 	anotherVariable = 2;  // Bzzzt!
}

int anotherVariable;

void loop() {
 	thisVariable = 1;
 	anotherVariable = 2;
}

a7

purplegorilla:
Secondary question: I actually don't understand how the code for the the almostEqual works, and the book says nothing of it.

Comparing for absolute equality of floating point numbers is usually not what you want to do. The canonical approach is to check that two numbers are within a certain offset of one another in order to determine equality. That's what almostEqual() is implementing.
ref: Question 14.5 for a bit more context.

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