Position of a function ...

I created a function that work very well if it's placed before the loop() but it's refused to be compiled if it's after it

Why?

jo_6466:
I created a function that work very well if it's placed before the loop() but it's refused to be compiled if it's after it

Why?

Can you prove it by showing us the code?

Paul

You will need to give the error message as well.

I suspect the following

C/C++ compilers check when they encounter a call to a function if what you provided as arguments are of the correct type. So they need to know what the function looks like and the usual way is to put the function before loop() or to provide a prototype (so it can be used in multiple files).

You can add the function prototype to the top of your code.

// prototype
void someFunction (int a, int b);

void setup()
{
}

void loop()
{
  someFunction();
}

void someFunction (int a, int b)
{
  ...
  ...
}

The Arduino IDE uses a builder that tries to do the hard work for you and finds the functions in your code and inserts the prototypes. The resulting file is a temporary file (extension .ino.cpp) that you can find on your computer.

Makes it easier for beginners but makes programmers that don't understand what is actually is needed. Sometimes the builder drops a stitch and did e.g. not add the prototype. So you're lucky and now know what a real program should look like :wink:

Suspect that the op is NOT using the IDE or that we are not talking about an ino file.

Remember that the IDE adds fuction prototypes to get round C/C++ rule if declare before use.

Mark

Correction:

holmes4:
Remember that the IDE tries to adds function prototypes to get round C/C++ rule if declare before use.

But it would not be the first time I see it fail.

septillion:
Correction:
But it would not be the first time I see it fail.

Amen

Hi Sterretje

My function name is : int mesure_th(float& Temp,float& Humidity,int nb2)

The error code is : 'mesure_th' was not declared in this scope

After to put as suggested by you before setup() int mesure_th(float& Temp,float& Humidity,int nb2); this error is out but i have now an another : a function-definition is not allowed here before '{' token

And the code as requested by Paul_KD7HB in the first reply?

sterretje:
And the code as requested by Paul_KD7HB in the first reply?

it's very long!

jo_6466:
it's very long!

Then make a small, simple program that demonstrates the problem.

Paul

Paul_KD7HB:
Then make a small, simple program that demonstrates the problem.

Paul

Ok i prepare it

jo_6466:
Ok i prepare it

Strange ... when i reproduce the function (simplified) i don't have the problem anymore
It remain me to relook entirely my original sketch
I return to you if i not find what is wrong

/***************************************************************
bibliothèques et paramètre de mesure de temperature par DHT22
****************************************************************/
#include <Adafruit_Sensor.h>
#include "DHT.h" // Librairie des capteurs DHT
#define DHTPIN 9 // Mesure sur la pine 9
#define DHTTYPE DHT22 // DHT 22 (AM2302)

float T=0;
float H=0;
float Temp=0;
float Humidity=0;
int nb2=0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
mesure_th (10); // le seul paramètre à transmettre est nb2=10 avec retour demandé T et H

Temp = T;
Humidity = H;

Serial.println (Temp);
Serial.println (Humidity);
Serial.println ();
}

/***************************
FONCTION MESURE TEMPERATURE
**************************/
int mesure_th (int nb2)
{
DHT dht(DHTPIN, DHTTYPE);
dht.begin();

int z=0;
float mesureT=0;
float mesureH=0;
float moyenneT=0;
float moyenneH=0;

/*************************************************************************************
Mesure de nb2 echantillons temperature et humidité sur 1 sec puis calcul des moyennes
*************************************************************************************/
for (z = 1; z < nb2; z++)
{
float mesureH = dht.readHumidity(); // Lecture du taux d'humidité
//Serial.println(mesureH);

float mesureT = dht.readTemperature(); // Lecture de la température en Celcius
//Serial.println(mesureT);

if (isnan(mesureH) || isnan(mesureT)) // Stop le programme et renvoie un message d'erreur si le capteur ne renvoie aucune mesure
{
Serial.println("Echec de lecture !");
return; // on arrete les mesures et on revient au programme principal
}
moyenneT = moyenneT + mesureT; // sommation des 10 mesures de T°
moyenneH = moyenneH + mesureH; // sommation des 10 mesures d"humidité
delay(100); // chaque mesures espacées de 100ms pour une mesure totale de 1 secondes
}

T = moyenneT / (nb2 - 1); // calcul de la moyenne des temperatures mesurées
H = moyenneH / (nb2 - 1); // calcul de la moyenne des humidités mesurées

return (T, H); // renvoie les mesures Temp et humidity vers le programme principal
}

Next time, use code tags please.

But the "a function-definition is not allowed here before '{' token"-error is usually caused but not keeping track of the brackets (poor indentation is a huge factor) because you try to define a function inside another function. Aka, check ALL brackets :slight_smile:

septillion:
Next time, use code tags please.

But the "a function-definition is not allowed here before '{' token"-error is usually caused but not keeping track of the brackets (poor indentation is a huge factor) because you try to define a function inside another function. Aka, check ALL brackets :slight_smile:

Please, how to use code tags? ... i'm new on this forum

That's why there is a big fat sticky at the top of each board called How to use this forum. Can't make it any easier....