SCOPE
Just to make it worse, any variables you toss around and manipulate throughout your program have scope, which determines when they exist, and contain valid values. Scope may be global, local to a function, or local to any { block of code }
If you access a variable outside of it's valid scope, you'll get a compiler error.
I did not want to put this in "tutorials" , but have to ask.
Is term "global variable" in Arduino really global or just "local" in main(); ?
TolpuddleSartre:
I believe that the references to the anonymous functions are called "funct" and "square".
It's a subtle distinction, but an important one.
yes...
except that does not distinguish them. Just consider main, setup, and loop.
They are all pointers to a function.
funct and square just happen to have scope local to setup().
EDIT:
consider this equivalent code:
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
void setup() {
Serial.begin(9600);
void(*funct)(void) = [&]() {
for (auto& a : array) {
auto square = [&a]() {
Serial.print(F("initial a:\t"));
Serial.println(a);
a = a*a;
};
square();
};
};
funct();
Serial.println();
for (auto a :array) {
Serial.print(F("updated a:\t"));
Serial.println(a);
}
}
Setup() and Loop() are CALLED from the (unseen) main() function; they are NOT defined inside the main() scope.
[automatically typed pointers to lambda functions]
Whee! Congratulations!
You know more C++ than most arduino programmers (or even C++ programmers) ever use or need; a feature not defined until C++11, and not enabled/present (for example) in Arduino 1.0.6
Thank you SO MUCH for helping to confuse the beginners!
(I mean, I do appreciate having "real" C++ programmers around to explain occasional C++ features and issues, this was not the thread to bring up such an obscure point.)
I suppose you'll be admonishing poor @TolpuddleSartre as well?
Nah. "How could it be otherwise" is sort-of "typical level of rudeness" for the Arduino forums (sigh)...
his was not the thread to bring up such an obscure point.)
should I be checking in with you first?
I was hoping to inspire better judgement in the more knowledgeable contributors. "Know your audience."
If the original post indicates a confusion between "defined in" and "called from", it's not the time to bring up lambda functions.