Go Down

Topic: Order of functions matters? (Read 736 times) previous topic - next topic

dentrado

When I try to compile this example I had to put the freqout() function before loop(), otherwise I got the error:
Code: [Select]
In function 'void loop()':
error: 'freqout' was not declared in this scope

but according to the arduino reference the order shouldn't matter. Then why does it?



RuggedCircuits

I don't think the Arduino reference is right. Functions should be declared before they are used.

You can put an external declaration to your function near the top of your sketch:
Code: [Select]
extern void freqout(int freq, int t);Then you can put your function anywhere you like since you have told the compiler about the functions's parameters and return value, so it knows how to call it.

mem

#2
Jul 04, 2009, 06:04 pm Last Edit: Jul 04, 2009, 06:17 pm by mem Reason: 1
The Arduino reference is correct for simple functions in the main sketch or in pde files included as tabs. The build process creates the forward references needed so functions do not need to be declared before they are used. But this does not work for functions that have user defined types or function in c or cpp files and the suggestion to declare references  at the top of your sketch may be necessary.

Can you post a small example showing the code that does not compile?

Kitep

When I copied and compiled http://www.arduino.cc/playground/Main/Freqout it compiled just fine for me.  Did you modify the code somehow?

dentrado

No I didn't, but I noticed that I had a old version of the arduino software, so I downloaded the newest one and then it works without modification. So maybe declaring functions wherever you wan't is a feature added in a newer version. Anyway, thanks for all the help.

Go Up