This code segment (defines and include not included) returns an out of scope error for the call to myPattern() in the loop() function. I have tried placing the myPattern function before the setup fuction and between the setup and loop functions. I am using the latest version of IDE to compile and a UNO chip. Any suggestions regarding this error are appreciated.
post the whole code or an example demonstrating your issue.
C++ states that you must declare something before using it (and the IDE does a few tricks to help out) so you should have myPattern() declared before the loop since this is where you use it
My functions all reside after loop(), sometimes in separate tabs, sometimes within the same tab. My experience has been that these 'tricks' referenced above take care of the requirement, @Terrypin, in most cases. The IDE can get confused when you start using multiple tabs to segregate your code, but other wise does a good job (for my simpleminded code, anyway) creating the necessary function prototypes.
indeed the "training wheels" that Arduino provides do work OK most of the time
but better learn the supported / specification compliant way if you were to change IDE or write C++ for something else
OK, thank both, understood. I’ll try to break my habit then. Might also get around to one other task, arranging them in alphabetic order. Even with say only a dozen functions, scrolling to find one can be slow. (I sometimes open the Find box instead.)
If one function includes calls to others are there any pitfalls I should be aware of?
No. It really doesn't matter at all where you put stuff or whether calls go up or down in the source code, or whether they go off to other tabs and stuff. Except to humans as an aesthetic matter.
As long as everything is in there somewhere and there are no missing parts or contradictory definitions, everything will get found and linked up properly.
I use find all the time. I refer to my source code as "the soup", s'all good, all in there somewhere floating around.
the IDE will generate basically the forward declaration for you so you are safe (using .ino files)
in general if functionC calls functionB which calls functionA then it's best to declare functionA first, then functionB and then functionC.
void functionA() {•••} // at this point the compiler knows functionA
void functionB() {••• functionA(); •••} // so it can be called.
void functionC() {••• functionB(); •••}
if you are unsure generate the forward declaration yourself at the top of the file
// declare the functions so that the compilers knows about them
void functionA();
void functionB();
void functionC();
// then you can define them in any order
void functionB() {••• functionA(); •••}
void functionC() {••• functionB(); •••}
void functionA() {•••}
Straying OT, took me a while to truly grasp the value of breaking my code into functions. The mentions here of tabs has me wondering if I should be using those too?
I would use tabs for *.cpp and *.h files. If you only use them for *.ino files, any advantages that would mean in a normal development environment are thrown out the window.
All the *.ino are concatenated into one source file. Since that happens anyways, putting things in tabs is just again an aesthetic choice.
I use one *.ino file, everything is in there somewhere and it does get messy.
If it helps you organize and be able to ignore whole sections of code that are working and somehow related, use tabs.
I am disorganized, and for me tabs only make that worse.
FWIW - I use one tab for LCD-related stuff, one for my serial processing, one for my EEPROM management, one for input management, one for output management, one for all my pre-setup() plus setup() plus main(). More generally, related functions get grouped. There's usually a "misc" tab in there, too, until it gets too long, then I try to grab a group of related functions and move them to their own basket...
It's really a hoarder's habit. If I can't find it, I try to figure out where my squirrel-brain will look for it next time.