I have noticed that I can compile my user defined functions in the void setup() only if they are in logic as below. So sometimes I will wrap my custom function in dummy logic just to get it to execute. Why will custom functions outside of logic not compile in the void setup in Arduino IDE?
int a = 1;
if (a ==1){
function();
}
else{
//do nothing
}
It is an error on my part it appears there was something wrong in my code but I can't post it easily it is 1000+ lines. But I just have a poor understanding on nested user defined functions
That is nonsense. Functions can be declared almost anywhere within a .ino file, except within another function. And the position within the file does not define the functions scope. The position relative to other code defines the functions scope. A function declared outside a class or struct is global. A function declared within a class or struct is not global.
This is the C++ norm that imposes that things are declared before they can be used . So having them at the top IS the best practice to learn for the long run.
The reason you can put thing the way you want in a .ino is because the IDE does add the forward declaration for you at the top (sometimes - but less often now - making a mess and introducing weird errors) but you don’t want to depend on training wheels your whole programming life… so learning to do it the proper way is a good idea
Bit strong of a word "nonsense", but for a post in response to a question on Arduino, I felt it appropriate as it the solution to the Op's inquiry without going into how the Arduino build system handles code arrangement and automatic prototyping. It was simply my choice to be concise and I take responsibility for not offering a more complete and wider-scope response.
I have found that teaching adults works best when answers are concise and do not go into a number of options and rules and history; folk just want answers on forums. Adults get bored really quickly when answers get muddy, complicated, or are too wordy.
We're not talking about standard c/c++ here, as setup() is explicitly mentioned. It makes NO difference whether functions are defined before or after setup(). None. They are global either way, so this statement is 100% false.
This suggests that declaring a function AFTER setup() will cause it to be optimized away, or otherwise "excluded" by the compiler. That is 100% false. Where a function is declared in a .ino file has NO impact on optimization. It will be optimized away ONLY if it is never called, or if it has no side-effects.
It is simple. You can't use nested define functions in the C/C++ language. It won't allow it, so don't try it or try to get round it. That is all you need to know about the topic.
If you want to understand why then you have to know a lot more about how the language is compled than you do now. Probably not worth it unless you want to define a compiler.
Ray:
Let it drop. I certainly explained my rational earlier on the "why" I gave the Op a correctly worded, 100% correct answer; correct answer being an answer that both "works and is repeatable." I could have stated to put user functions into a separate IDE tab that has the default .ino extension.
I honestly appreciate your giving a diverse answer to the Op, and Op may or may not care as they may just as well have a single answer to remember. I do think however that you could have broadened the Op's knowledge without casting an attack on another senior member - I took it as unsportsmanlike and out of character with the value you have provided the forum with for almost a decade.
I don’t see why setup() means it is not c++.
Sketch are written in c++, the IDE brings just the training wheels and libraries an help to abstract common hardware
In standard c++ the position of the function declaration in the file makes it known to functions further down in the file, not above. So they are not « global » in scope per se.
That being said
Otherwise, the compiler will see an unused function and exclude it