Use multiple variables from one function in another

Hi all
I am trying to make variables generated in one function available in other functions. I searched through the forum for a good while now, and while there are many good suggestions, I can't get it right. Hence, the new topic...

I would like to read two or more analog values in one function, and then use these values in another function. As I understand, C can not send multiple values between functions without usin e.g. a struct? Any help would be much appreciated (see the attached code).

Many thanks in advance!

const int analogPin1 = A0;  // Analog input pin for sensor1
const int analogPin2 = A1;  // Analog input pin for sensor2
int sensorValue1 = 0;        // value read from sensor1
int sensorValue2 = 0;        // value read from sensor2


void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  byte data[2];
  analogMeasurement(data); //here I would like to "import" the values generated in "analogMeasurement"
  Serial.println(data);
  delay(1000);
}

void analogMeasurement(byte data[]) 
{
  // read the analog values:
  sensorValue1 = analogRead(analogPin1);
  sensorValue2 = analogRead(analogPin2);
  
  data[0] = sensorValue1;
  data[1] = sensorValue2;
  }

As I understand, C can not send multiple values between functions without usin e.g. a struct,

Then you understand wrongly. What is true is that C cannot return more than one variable from a function, but there are ways round that.

The easiest way is to use global variables (sharp intake of breath from some forum members) then you do not need to pass the variables around. You are already using global variables in your program so why not extend their use ?

As I understand, C can not send multiple values between functions without usin e.g. a struct

That is NOT true.

   byte a = 1;
    byte b = 2;
    byte c = 3;

    someFun(a, b, c);

    Serial.print("a = ");
    Serial.println(a);
    Serial.print("b = ");
    Serial.println(b);
    Serial.print("c = ");
    Serial.println(c);

void someFun(byte &a, byte &b, byte &c)
{
    a *= 10;
    b *= 10;
    c *= 10;
}

The output will be a = 10, b = 20, c = 30.

Look up "pass by reference".

Using an array will work, too. Typically, the function is declared a bit differently, though.

void analogMeasurement(byte *data)
{
  // read the analog values:
  data[0] = analogRead(analogPin1);
  data[1] = analogRead(analogPin2);
}

However, since you are using global variables in the function, there is no real harm in storing the output of analogRead() in global variables, too, eliminating the need to pass anything to or from the function.

The easiest way is to use global variables (sharp intake of breath from some forum members)

No sharp intake here, because you DID say "easiest" rather than "best". Global variables are not the best solution, but they are acceptable in some situations. This appears to be one of them, since OP is already using global variables in the function.

Thank you both for clarifying this to me. I will try to implement the suggestions from PaulS and report back. And yes i agree, using global variables would be the easiest thing for me here.
Thanks again

I have edited my initial post to avoid further misunderstandings.

In doing so you have made our replies look like nonsense.

It really doesn't make sense to store the data in global variables AND in an array exchanged between the caller and the function. But, whatever floats your boat.

What IS the problem with the code you posted? What does it actually do? How does that differ from what you want?

OP must be aware that he is assigning ints to bytes, here but why?

 sensorValue1 = analogRead(analogPin1);
  sensorValue2 = analogRead(analogPin2);
  
  data[0] = sensorValue1;
  data[1] = sensorValue2;

What IS the problem with the code you posted? What does it actually do? How does that differ from what you want?

The initial problem was that the analog values were = 0 when they returned in the "void loop", even though they got a reading/value in "void analogMeasurement". Therefore I thought I had to do something in addition to using global variables. After a reset of the Arduino, I got the correct readings in the "void loop". I think it might be because I initially had written int sensorValue1 and not int sensorValue1 = 0 etc.
So you were right, it did not differ from what I wanted, except that the array exchange was not necessary.

OP must be aware that he is assigning ints to bytes, here but why?

It was an attempt to construct an array with the variables, so they could be accessible to other functions. It turned out to be an error.

Thanks again for the quick replies! Very helpful

Global variables have default value 0 if they are not initialized.

PaulS:
No sharp intake here, because you DID say "easiest" rather than "best". Global variables are not the best solution, but they are acceptable in some situations. This appears to be one of them, since OP is already using global variables in the function.

"best" is relative in that it depends on what you value the most.
In some situations globals are the best solution from a code size and performance perspective.
For example, on the AVR, using globals, particularly if they are for data structures, will generate significantly smaller code that is faster than a local since the AVR is so wimpy when it comes to addressing modes.

The compiler & linker will generate absolute addresses for global structure members whereas for locals it has to use offsets from a pointer and the AVR pretty much sucks at doing that.

And for source level debugging, globals can be beneficial as you can set breakpoints/traps when the global is accessed which will trip regardless of where it is accessed in the code.

--- bill