Possible to pass variables from several functions to one function?

Hello all!

I am working on a project to mesure several conditions: temperature, humidity, etc.

All the different parts and sensors are within their own function. Some contain global variables and some local variables as well.

The serial output of the variables should be in its own function to be better able to control it.

For the global variables there shouldn't be any problem. How can I how ever pass the local variables of the different functions to the serial output function? Is it possible? How best to do it? Any alternatives?

Thank you in advance!

moses

If the sensor reading only makes it to a local, you will need to have the function return that value. The serial print function can call to get it.

How can I how ever pass the local variables of the different functions to the serial output function?

When you call a function you can pass to it any variables that are in scope when you call it. If you call it from within your other functions then the variables local to those functions will be in scope and can be passed to your serial output function.

If that doesn't answer your question then please clarify the question with some sample code.

One approach is to have a global struct.
Then fill it with data in each function.
At the end of each loop, you should have all relevant data in that struct and use them.

wildbill:
If the sensor reading only makes it to a local, you will need to have the function return that value. The serial print function can call to get it.

Let's say I have a function with 2 temperature sensors. I would make a local struct to be able to return the whole struct? Is there a way to decide which of the struct members to get when I call the function with serial print? Or select it after having called it or while calling it?

arduino_new:
One approach is to have a global struct.
Then fill it with data in each function.
At the end of each loop, you should have all relevant data in that struct and use them.

But wouldn't that mean that my local variables would be saved twice? And therefore use space for a local variable and a global struct?

You should take a look at how the Adafruit Unified Sensor Library works.