-1

I define prototypes prior to calling the functions.

Does the order of the prototypes have to match the order of the functions in the code?

I'd like to sort the prototypes alphabetically, while I wouldn't care in which order they appear in the code.

I am after common or best practice.

closed as off-topic by Juraj, per1234, VE7JRO, sempaiscuba, MatsK Apr 22 '18 at 13:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – Juraj, per1234, VE7JRO, sempaiscuba, MatsK
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Why don't you try it and find out? – Mark Smith Apr 22 '18 at 5:01
  • I am after common or best practice... and what a lame/flippant way to reply (and with a down vote)... I can try a lot of things, but would rather stick to what is considered good practice. – MaxG Apr 22 '18 at 5:02
  • Your question as you originally posted it, with no mention of best practice, deserved the down vote, in my opinion: you could have tested whether the order mattered very easily, and prior research is expected here. With your edits there's now a sensible question, and I'll remove my downvote. You shouldn't take this personally - it's about the quality of the question, not a slight on your character. – Mark Smith Apr 22 '18 at 5:14
  • 1
    how is this C question arduino related? you don't need function forward declaration in ino files (unless you use a pointer to function before setup()). – Juraj Apr 22 '18 at 5:46
  • @Juraj: "unless"... and then you need to. The Arduino is programmed in C, hence it is Arduino related (so I thought). – MaxG Apr 22 '18 at 6:02
0

There is no specific best practice in this regard.

The rest of this answer is primarily opinion based, and hence off topic for this site.

If you have classes it's common, but in no way required, universal or "bad" not to do, to group the public, private and protected methods together.

Really, group them in a way which makes sense for your program - related functions together. If you have so many methods / functions that they need a lot of organizing, split them into groups of related functionality - using classes or whatever.

  • It is sometimes a catch 22 to determine, whether a question will have a sort of standard answer or only an 'opinion'ated one. :) – MaxG Apr 22 '18 at 6:30
1

Does the order of the prototypes have to match the order of the functions in the code?

No.

I'd like to sort the prototypes alphabetically, while I wouldn't care in which order they appear in the code.

You can do that. It would make the prototype easier to find, I suppose, however most editors these days have search functions.

I am after common or best practice.

Because the Arduino IDE automatically generates function prototypes the common practice is not to use them at all.

If you want to know best practice in general I suggest you ask on Stack Overflow.


Years ago I programmed Burroughs Medium Systems in BPL (Burroughs Programming Language), a language similar to Algol (which Pascal looks a bit like).

In BPL you could not use a function or variable which had not been previously defined. Thus, the "main" function would always appear at the end of the source file. Major functions (called by the main function) would therefore precede it, and minor functions would appear earlier again. Thus you could tell the hierarchy of function calls, roughly, by the order in which the functions appeared.

In C (and C++) you don't need function prototypes if you have all your code in a single file, and if you follow that convention. Put the main code at the end and the things it calls before them. You only need function prototypes then in unusual cases where A might call B but B might call A.

Not the answer you're looking for? Browse other questions tagged or ask your own question.