How to use functions?

I’m trying to build a simple code in Arduino IDE - 1.0-beta1 ( Arduino SDK 1.0.5 ),
but sofar unsuccessfully.

I download the code:

int trigPin = 2;
int echoPin = 4;
 
void setup() {
  Serial.begin(9600);
}
 
void loop(){
  long duration;
  float cm;
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
} 
float microsecondsToCentimeters(long microseconds){
  return (microseconds*0.034029)/2;
}

but get an error when trzing to build it:
error: ‘microsecondsToCentimeters’ was not declared in this scope
Compilation failed.

I red the http://www.arduino.cc/en/Reference/FunctionDeclaration
but still don’t understand what is here the problem?
Wat am I missing here?

ultrasonic_distance.ino.ino (564 Bytes)

The posted code compiles with no errors on IDE version 1.0.5.

Compiles for me....

Arduino IDE - 1.0-beta1 ( Arduino SDK 1.0.5 ),

Compiles fine for me too. That's a rather odd version string; are you using some IDE other than the one from arduino.cc? The error you're seeing is about what I'd expect if something was NOT doing the usual pre-processing of .ino files into .cpp files.

You can probably "fix" it by moving your function to BEFORE the loop() definition.

westfw:

Arduino IDE - 1.0-beta1 ( Arduino SDK 1.0.5 ),

Compiles fine for me too. That's a rather odd version string; are you using some IDE other than the one from arduino.cc? The error you're seeing is about what I'd expect if something was NOT doing the usual pre-processing of .ino files into .cpp files.

You can probably "fix" it by moving your function to BEFORE the loop() definition.

Yes, I fix it by moving the function BEFORE the loop() definition. Than you very much.

Thank You all for responses.

Regards, from Pál