"function-definition is not allowed" error

I am trying to run this code:

int pingPin = 7;
int speakerPin = 9;

int length = 100; // the number of notes
char notes[] = "CgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCg"; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names == note) {
_ playTone(tones*, duration);_
_
}_
_
}_
_
}_
void setup(){
_
pinMode(speakerPin, OUTPUT);_
_
}_
void loop()
_
{_
_
long duration, inches, i;_
_
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds._
_
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse._
_
pinMode(pingPin, OUTPUT);_
_
digitalWrite(pingPin, LOW);_
_
delayMicroseconds(2);_
_
digitalWrite(pingPin, HIGH);_
_
delayMicroseconds(5);_
_
digitalWrite(pingPin, LOW);_
_
// The same pin is used to read the signal from the PING))): a HIGH*_
* // pulse whose duration is the time (in microseconds) from the sending*
* // of the ping to the reception of its echo off of an object.*
* pinMode(pingPin, INPUT);*
* duration = pulseIn(pingPin, HIGH);*
* // convert the time into a distance*
* inches = microsecondsToInches(duration);*
* if (inches <= 10) i == 1*
* ;for (int i; i < length; i) {*
_ if (notes == ' ') {
delay(beats * tempo); // rest
* } else {_
playNote(notes, beats _ tempo);
* }*_

* // pause between notes*
* delay(tempo / 10);*
}
long microsecondsToInches (long microseconds)
{
* // According to Parallax's datasheet for the PING))), there are*
* // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per*
* // second). This gives the distance travelled by the ping, outbound*
* // and return, so we divide by 2 to get the distance of the obstacle. // See:http://www.parallax.com/dl/docs/prod/acc/2801 PINGv1.3.pdf*
* return microseconds / 74 / 2;*
* }*
and this error:
In function 'void loop()':
error: a function-definition is not allowed here before '{' token
comes up.
Can someone please tell me why?

[edit] First see Fjornir's comments below. He is correct.

either put the microsecondsToInches () function up above the setup() function or put a function prototype for it up there.

The compiler has to know what the function is that your using before you call it (just like a data variable).

I believe you will find that jafadmin is mistaken. The arduino IDE will automagically generate prototypes for the functions you use.

In your case it looks like you have unbalanced {}s. Run over your code again looking for a { where you don't have a } at the appropriate following spot. At a glance I found a missing }.

You are correct, Fjornir. Thanks for pointing out that difference from formal C.

I understand that the Arduino IDE will generate prototypes automatically but I'm not altogether sure that is a good thing. It does indeed make life simpler for a beginner/hobbyist however I'd argue that it teaches one learning C a bad habit that will come back to bIte her if she starts working in another environment that expects the C protocols to be adhered to.

Perhaps generating the prototypes automatically along with a warning message (explanatory - as a teaching tool) would be better.

Roy, Arduino is intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. (that is a direct quote from the arduino home page)

Things like auto-prototyping are there to insulate the user from programming details. There are plenty of C environments that are available for formal learning What makes the Arduino different is how easy it is to create physical computing projects with minimal programming knowledge.

I agree that the Arduino environment hides many technical aspects that someone interested in learning software engineering should learn (for example, most aspects of object oriented programming are hidden). But that is a good part of what makes it so popular.

I reviewed the brackets, placed one and the sketch is working now, but there is another poroblem that came up when the program was uploaded.
the pseaker only plays the first note over and over again.

you are missing the ++ at the end of your for loop, change:
for (int i; i < length; i) {
to
for (int i; i < length; i++) {

When posting code, please do not just paste it into the forum. Use the CODE button above the text box when entering text and paste it in between the commands that appear. Thanks.

OK I'll do that with the code, and I changed the i to i++. Now it works.