Function Prototypes - conclusively, sometimes it's needed?

Hi, I wasn't sure if I should post here or in the installation forum.

I have done my googl'in and I see that arduino theoretically makes function prototypes. However, in my larger sketches with many functions, I've found that I need to put function declarations in a linear order (where function A that is called by function B, is listed above function B in the linear order of the sketch.) Or, I have to make function prototypes.

Does the arduino software give up on making function prototypes for you when you have a larger sketch? What gives? Shouldn't this be done automatically?

I know I have solved my problem by making function prototypes, but I was hoping to solve the mystery of why it was necessary.

Thanks everyone.

What goes wrong?

If you pass user-defined types (structure or class) to a function and don't put in your own prototype, Arduino will put one at the top of the file BEFORE the declaration of the user-defined type and the prototype will fail.

The compiler/arduinoIDE will say that the function is not declared in this scope.

In the past I would make it so that function A that would be called in Function B would be placed before function B, in order to avoid this problem.

It wasn't until my Uncle, a primarily C/C++ programmer, mentioned function prototypes that I tried applying those instead. And that helped because in some cases you can't easily have that kind of "function A before function B" because sometimes the function calling is all over the place depending on different circumstances.

Let me know if I am not being clear enough. I'll try to clarify.

What you're describing is conventional C++ behaviour.

The Arduino does some mucking about with your code before compiling it, and among other things this is intended to provide the function prototypes for you. However, the implementation is rather mediocre and can be tripped up by relatively simple code. Perhaps that is what has happened in your case. If you posted an example sketch which demonstrates the problem, perhaps we could confirm whether that is the problem.

Forward declaration.

You may try defining the prototype in a .h tab, #include it in the main module.
(You may need to mark it extern)

mellis says,,40585.0.html

The problem is related to the fact that Arduino automatically generates prototypes for functions in your sketch. These prototypes are inserted above your code. Thus, MorseFlasher is not declared before those prototypes and you get an error. Typedefs are a feature of C/C++ that we thought might be too confusing for our target audience, so we haven't yet worried about supporting them. For now, you can try rewriting your code without using the typedef, or compiling it outside of Arduino (e.g. with the Arduino command line instructions at:

Edit: also

Typedefs are a feature of C/C++ that we thought might be too confusing for our target audience, so we haven't yet worried about supporting them.

Well intentioned but IMO misguided - I don't see how you could possibly justify not supporting something so basic to the C++ language, doubly so since this omission is not documented. The whole 'automatic prototype generation' thing is a long ongoing train wreck, and I hope that the Arduino development team will soon realise their mistake and enable us to turn it all off.