Really basic but strange problem

I'm not the most efficient code writer, but I do try to keep things logical.

I have a sketch that was starting to get a bit long and untidy, so I decided to break it down in to a number of functions, each of which I tidied away to it's own tab.

The sketch compiled and uploaded fine, or at least it did. I included a new function, again in it's own tab, but I am now getting error messages for some of the original functions, despite not having made any changes to them.

The error message I am getting is ''l2rWallSlide' was not declared in this scope'. l2rWallSlide is the name of the function, and I am getting the same error message for a lot of the other functions, though not all.

I have double checked that the functions are named correctly, and don't have any upper or lower case characters where they shouldn't be. But the strange thing is that I have not changed any of them since the sketch last compiled successfully so I am at a loss as to why I am getting this error message now.

How are the files for the extra tabs named ?

They use the same names as the function.

However, I seemed to have solved the problem. The new function I added actually called a second function, which is what appears to have thrown a spanner in the works.

I have re-edited the sketch to include the sub-function in to the body of the new function and it now compiles fine.

I’m guessing the Arduino environment doesn’t like nested functions.

I'm guessing the Arduino environment doesn't like nested functions.

It does not. You can have multiple function CALLS though, nested as deep as you like.

The IDE combines all of your sketches to produce one cpp file that it passes to the compiler.

The order of the sketches matches the order of the tabs, NOT any logical order. The tab order depends on the name of the file in the tab, so you can impose order by naming the tabs so that they are sewn together in the proper "order".

Or, define your own function prototypes, in the main sketch tab, and then the order won't matter.

That would appear to be the problem as I have just realised that my existing functions already use multiple calls to other functions.

Just need to work out what order the tabs need to be in now.

Just need to work out what order the tabs need to be in now.

You did not fully answer my previous question about tab/file names. What extension have you used for the file names ?

I think that you will find that the files are compiled in alphabetical order

UKHeliBob

The tabs have been named identically to the functions contained within. I have tried to name the functions as descriptively as possible to remind me what each of them does. As a result they are not called in alphabetical order, nor do they appear in alphabetical order across the tab list at the top of the IDE screen.

When you have several .ino files in a project the Arduino IDE first loads the principal project file and then loads the other files in alphabetical order.

Variables defined in a early file can be accessed by code in a file loaded later, but not vice versa. I can’t recall whether that is also true of functions. If it is it may help to put function prototypes in the principal file. For a function like this

void myFunction() {
   Serial.println("this is myFunction");
}

the function prototype will be

void myFunction();

…R

As a result they are not called in alphabetical order

If they are .ino files they are, which is why I asked what the file extensions are

Robin2:
I can't recall whether that is also true of functions.

Generally it should not be. The Arduino IDE adds prototypes for all functions in .ino files at the top of the first .ino file. Sometimes function prototype generation doesn't work correctly but I've never seen that be caused by putting files in tabs. It's more likely that a prototype was never correctly generated but it didn't matter before since the function was defined before it was called. After the sketch was split into tabs this was no longer the case and so it exposed the prototype generation failure.

pert:
Generally it should not be. The Arduino IDE adds prototypes for all functions in .ino files at the top of the first .ino file. Sometimes function prototype generation doesn't work correctly but I've never seen that be caused by putting files in tabs. It's more likely that a prototype was never correctly generated but it didn't matter before since the function was defined before it was called. After the sketch was split into tabs this was no longer the case and so it exposed the prototype generation failure.

One can, of course, enable verbose mode during compilation, and look at the resulting cpp file with function prototypes added, and remove all doubt.