Pages: [1]   Go Down
Author Topic: Order of functions matters?  (Read 550 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I try to compile this example I had to put the freqout() function before loop(), otherwise I got the error:
Code:
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?


Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: July 04, 2009, 11:17:57 am by mem » Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: