For some reason, in the ARDUINO IDE I am able to define a function before OR after it is called. I recently started programming in plain old C++ to improve my basic programming understanding/capabilities and you can't call a function that is defined later in the code, it must be define BEFORE it is called. This makes basic logical sense, what I don't understand is why it's not only acceptable for compiler but also functional at runtime(with arduino). This shouldn't work, why does it?
If I am relying on circularity i.e. func1>func2>func3>func1, how can I work around the scope problem in C++/outside of the arduino environment.
The arduino IDE creates prototypes for you which can be good or bad. A prototype tells the compiler what to expect for a function so you can call it before it is defined. it works like this:
int myfunc(int a, double b); //<- note the semicolon. declares a function that returns int and takes two args.
void someotherfunc(void)
{
int a = myfunc(3,3.14159);
}
int myfunc(int a, double b) // note, no semicolon.
{
...
return 42;
}
It's an attempt to make C a little less intimidating for newcomers. Same thing with Loop and Setup. Ok when it works, very vexing when it creates baffling bugs.
Doesn't really seem like it would make it less intimidating to omit declaration, just obscures something simple and necessary for the programmer to understand. What kinds of problems can this cause?
I haven't seen it lately, but there was a version of the IDE that would do the prototypes incorrectly occasionally and you would need to declare an unnecessary variable at the start of your sketch to make the resulting weird errors go away.
C arose in the dark ages of computing when capabilities were far far less than what they are now.
C++ has carried forward many of those archaic rules. There is no reason, on a modern system, to enforce the rule that a function must be declared in a higher location in the file than it's first use. It's just that that was the rule in the beginning, and it's never been relaxed.
AncientOracular:
Doesn't really seem like it would make it less intimidating to omit declaration, just obscures something simple and necessary for the programmer to understand. What kinds of problems can this cause?
I have found two:
Functions with default parameters. You cannot have both the prototype and the definition with the default parameter in them, it must be in just the prototype. The IDE tries to make the prototype with the default value and doesn't remove it from the definition.
Callback variables. The prototype generator inserts the prototypes immediately before the first function definition (usually setup). Because most people partition their source file in the order 1) includes 2) globals 3) functions, the prototypes get put after the globals. If any of those globals are function pointers, the function they're assigned might be out of scope since the prototype if below the variable assignment.
Jiggy-Ninja:
C arose in the dark ages of computing when capabilities were far far less than what they are now.
C++ has carried forward many of those archaic rules. There is no reason, on a modern system, to enforce the rule that a function must be declared in a higher location in the file than it's first use. It's just that that was the rule in the beginning, and it's never been relaxed.
It is handy when you have multiple files. It allows you to use header files and since all functions normally have external scope, the compiler (actually the linker) can know what to do if it calls a function defined in another file.