Porque de definir uma função antes do SETUP

Caros,

Estou com uma dúvida (se calhar é porque não percebo nada disto)
Ando a fazer experiencias com uma bússula e o programa de teste tem uma função definida antes do setup() e todas as outras estão depois do loop().
Existe alguma razão para isto?

Aqui vai o código:

/code
#include "Wire.h"
#include "HMC5883L.h"

HMC5883L compass; //Copy the folder "HMC5883L" in the folder "C:\Program Files\Arduino\libraries" and restart the arduino IDE.

float xv, yv, zv;

//calibrated_values[3] is the global array where the calibrated data will be placed
//calibrated_values[3]: [0]=Xc, [1]=Yc, [2]=Zc
float calibrated_values[3];
//transformation(float uncalibrated_values[3]) is the function of the magnetometer data correction
//uncalibrated_values[3] is the array of the non calibrated magnetometer data
//uncalibrated_values[3]: [0]=Xnc, [1]=Ync, [2]=Znc
void transformation(float uncalibrated_values[3])
{
//calibration_matrix[3][3] is the transformation matrix
//replace M11, M12,..,M33 with your transformation matrix data
double calibration_matrix[3][3] =
{
{1.454, 0.102, -13.718},
{-0.094, 1.106, -121.059},
{0.081, 0.076, 54.453}
};
//bias[3] is the bias
//replace Bx, By, Bz with your bias data
double bias[3] =
{
69.825,
295.451,
-184.34
};
//calculation
for (int i=0; i<3; ++i) uncalibrated_values = uncalibrated_values - bias*;*
* float result[3] = {0, 0, 0};*
* for (int i=0; i<3; ++i)*
* for (int j=0; j<3; ++j)*
result += calibration_matrix[j] * uncalibrated_values[j];
for (int i=0; i<3; ++i) calibrated_values = result*;*
}
void setup()
*{ *
* Serial.begin(9600);*
* Wire.begin(); *
* compass = HMC5883L(); *
* setupHMC5883L(); *
}
void loop()
{
* float values_from_magnetometer[3];*

* getHeading();*
* values_from_magnetometer[0] = xv;
values_from_magnetometer[1] = yv;
values_from_magnetometer[2] = zv;
transformation(values_from_magnetometer);
_ Serial.flush();_
Serial.print(calibrated_values[0]);
_ Serial.print(",");_
Serial.print(calibrated_values[1]);
_ Serial.print(",");_
Serial.print(calibrated_values[2]);
_ Serial.println();
delay(100);
}
void setupHMC5883L()
{
compass.SetScale(0.88);_

compass.SetMeasurementMode(Measurement_Continuous);
_}*_

void getHeading()
{
* MagnetometerRaw raw = compass.ReadRawAxis();*
* xv = (float)raw.XAxis;*
* yv = (float)raw.YAxis;*
* zv = (float)raw.ZAxis;*
}
code/
Obrigado por qualquer explicação.

Não sei onde foi buscar esse programa, mas à primeira vista eu digo que não funciona, nem com a função antes do setup, nem com ela depois do setup. Por exemplo esta parte:

  for (int i=0; i<3; ++i)
    for (int j=0; j<3; ++j)
      result += calibration_matrix[j] * uncalibrated_values[j];

é definido o contador 'i', mas ele nunca é usado.

Neste IDE, penso que é indiferente o local onde é colocada a função. Ou seja, tanto pode ser colocada antes do setup, como depois do loop (como em qualquer local entre estes dois pontos). Em linguagem C "pura", isto não é bem assim. Normalmente ou se define a função antes do local da sua utilização, ou então, se coloca no inicio do programa o protótipo de todas as funções que estão definidas nesse módulo.

Depois de testar, este programa tanto compila assim:

#include "Wire.h"
#include "HMC5883L.h"

HMC5883L compass; //Copy the folder "HMC5883L" in the folder "C:\Program Files\Arduino\libraries" and restart the arduino IDE.

float xv, yv, zv;

//calibrated_values[3] is the global array where the calibrated data will be placed
//calibrated_values[3]:=Xc, [1]=Yc, [2]=Zc


float calibrated_values[3];   
//transformation(float uncalibrated_values[3]) is the function of the magnetometer data correction
//uncalibrated_values[3] is the array of the non calibrated magnetometer data
//uncalibrated_values[3]: =Xnc, [1]=Ync, [2]=Znc



void setup()
{   
  Serial.begin(9600);
  Wire.begin(); 
  compass = HMC5883L(); 
  setupHMC5883L();       
}

void loop()
{
  float values_from_magnetometer[3];
 
  getHeading();
  values_from_magnetometer[0] = xv;
  values_from_magnetometer[1] = yv;
  values_from_magnetometer[2] = zv;
  transformation(values_from_magnetometer);

  Serial.flush();
  Serial.print(calibrated_values[0]);
  Serial.print(",");
  Serial.print(calibrated_values[1]);
  Serial.print(",");
  Serial.print(calibrated_values[2]);
  Serial.println();

  delay(100);
}

void setupHMC5883L()
{ 
  compass.SetScale(0.88);
  compass.SetMeasurementMode(Measurement_Continuous);
}
 
void getHeading()
{
  MagnetometerRaw raw = compass.ReadRawAxis();
  xv = (float)raw.XAxis;
  yv = (float)raw.YAxis;
  zv = (float)raw.ZAxis;
}


void transformation(float uncalibrated_values[3])   
{
  /*
  //calibration_matrix[3][3] is the transformation matrix
  //replace M11, M12,..,M33 with your transformation matrix data
  double calibration_matrix[3][3] =
  {
    {1.454, 0.102, -13.718},
    {-0.094, 1.106, -121.059},
    {0.081, 0.076, 54.453} 
  };
  //bias[3] is the bias
  //replace Bx, By, Bz with your bias data
  double bias[3] =
  {
    69.825,
    295.451,
    -184.34
  }; 
  //calculation
  for (int i=0; i<3; ++i) uncalibrated_values = uncalibrated_values - bias;
  float result[3] = {0, 0, 0};
  for (int i=0; i<3; ++i)
    for (int j=0; j<3; ++j)
      result += calibration_matrix[j] * uncalibrated_values[j];
  for (int i=0; i<3; ++i) calibrated_values = result;
*/
}

como assim:

#include "Wire.h"
#include "HMC5883L.h"

HMC5883L compass; //Copy the folder "HMC5883L" in the folder "C:\Program Files\Arduino\libraries" and restart the arduino IDE.

float xv, yv, zv;

//calibrated_values[3] is the global array where the calibrated data will be placed
//calibrated_values[3]:=Xc, [1]=Yc, [2]=Zc


float calibrated_values[3];   
//transformation(float uncalibrated_values[3]) is the function of the magnetometer data correction
//uncalibrated_values[3] is the array of the non calibrated magnetometer data
//uncalibrated_values[3]: =Xnc, [1]=Ync, [2]=Znc


void transformation(float uncalibrated_values[3])   
{
  /*
  //calibration_matrix[3][3] is the transformation matrix
  //replace M11, M12,..,M33 with your transformation matrix data
  double calibration_matrix[3][3] =
  {
    {1.454, 0.102, -13.718},
    {-0.094, 1.106, -121.059},
    {0.081, 0.076, 54.453} 
  };
  //bias[3] is the bias
  //replace Bx, By, Bz with your bias data
  double bias[3] =
  {
    69.825,
    295.451,
    -184.34
  }; 
  //calculation
  for (int i=0; i<3; ++i) uncalibrated_values = uncalibrated_values - bias;
  float result[3] = {0, 0, 0};
  for (int i=0; i<3; ++i)
    for (int j=0; j<3; ++j)
      result += calibration_matrix[j] * uncalibrated_values[j];
  for (int i=0; i<3; ++i) calibrated_values = result;
*/
}

void setup()
{   
  Serial.begin(9600);
  Wire.begin(); 
  compass = HMC5883L(); 
  setupHMC5883L();       
}

void loop()
{
  float values_from_magnetometer[3];
 
  getHeading();
  values_from_magnetometer[0] = xv;
  values_from_magnetometer[1] = yv;
  values_from_magnetometer[2] = zv;
  transformation(values_from_magnetometer);

  Serial.flush();
  Serial.print(calibrated_values[0]);
  Serial.print(",");
  Serial.print(calibrated_values[1]);
  Serial.print(",");
  Serial.print(calibrated_values[2]);
  Serial.println();

  delay(100);
}

void setupHMC5883L()
{ 
  compass.SetScale(0.88);
  compass.SetMeasurementMode(Measurement_Continuous);
}
 
void getHeading()
{
  MagnetometerRaw raw = compass.ReadRawAxis();
  xv = (float)raw.XAxis;
  yv = (float)raw.YAxis;
  zv = (float)raw.ZAxis;

}

Não me perguntem porquê mas o i entre parenteses retos não me passa no copy/past.

mas juro que ele está lá:

Obrigado Luis,

Ou seja, tanto faz.
Eu pensei que pelofacto de haver duas matrizes com valores fixos onde se operam operações matemáticas
função de outra matriz que é dinamica (as leituras ao compass) isso fosse a razão. Como normalmente as variáveis são definidas logo no inicio...

Bem, sendo assim, posso arrumar "esteticamente" melhor o meu programa e pôr essa função depois di loop onde tenho todas as outras funções.

Só puz a questão porque foi a primeira vez que vi e achei estranho.

Obrigado mais uma vez.