Just a quick question about esthetics that really is unecessary, but I like to know these sorts of stuff.
If you refer to a "help method" in another method in arduino, would you prefer to write the help method before or after that method refering to it?
In other languages, I would usually have my help methods in a separet sketch/file, so I have not really come across this before.
My intuitive choice would be to have the help method after, but I could also see the point of understanding a method before its reference to avoid scrolling up and down in the code. What do you think?
Best solution would be to have a forward declaration (prototype) at the top. And implementation below your main sketch (setup and loop).
You can make a forward declaration like this:
int square(int x);
And then your implementation at the bottom:
int square(int x) {
return x*x;
}
This way you have sufficient info on how to use the function beforehand and saves a lot of scrolling to get to your main code.
Normally a prototype is needed if you want to put your implementation after your code. The Arduino team decided that is handy to generate prototypes automatically. So it is not needed to add them. And people are lazy and many beginners unknowing, so prototypes are often omitted. Still it is seen as good practice...
Helper functions can also be put in a separate .cpp file accompanied with a .h file where the prototypes are. This is very handy with larger programs and allows easy reuse of code.
Oh, that's great to know. So what is needed for the prototype? And what is meant by an accompanied .h file - just for them to have the same name to work?
My project is divided into two parts, with slight differences. So I will upload the two sketches to two arduino boards. The sketches share many methods so would be great to put some of that code in a different file.
Thanks for baring with me, I have close to no experience in C++.
In the IDE in the right upper corner there is an arrow.
If you click, you can add new files/tabs to your sketch.
You should make 2:
xxx.cpp and xxx.h
The .h file should start with:
I am not sure what you mean by "help method", but when it comes to documentation, you could have a look at Doxygen. It is a standard for generating documentation from code. You can compare this to what Sphinx is for in Python.
For example, the following documentation is generated from this source file.
I honestly have no clue what OP is talking about. Prototypes...Helpers...Scrolling down... Why dont you open a popular library and see how it is arranged?
I used a made up term "help method" - since I could not come up with any other fitting word - and described what I meant by it, please try to read again if you don't get it. Scrolling down is when you move further down on a page with your mouse on a computer. And prototype was a word someone else used. Anything else that is unclear?
While I absolutely approve of using the tools from the software engineering toolbox on any project (it's amazing how often they stop you shooting yourself in the foot), Arduino programs are usually so small that I've never felt the need to split them into multiple files.
I know that other folks here like to have several tabs but on something with under a thousand lines of code, I'll just keep it where I can see it.
Hey man, you are testing my patience right now. My original question was if method declarations in arduino usually is before or after the method where it is used (methods other than loop and setup). Since you can declare a method both before and after it's used, I would say that this has to do with esthetics - how do you want your code to look to make it as readable as possible.
Please refrain from replying if you are just going to quibble instead of contributing to the discussion.
That is untrue. A "method" (in C++ it's called a function) must be at least be "declared" (prototype provided) before it is used. It may be implemented after it is used. If it's implemented before the first use, a prototype is unnecessary.
Please note that the Arduino toolchain automatically generates prototypes for functions inside of an ino file and puts these at the top of the sketch. This may give the appearance that the order of declaration and definition is irrelevant.