Go Down

Topic: Can you call a variable from an object in another tab? (Read 629 times) previous topic - next topic

khansen3


Regardless of whether the function are in different tabs or the same tab, any variable declared within a function can never be referenced in another function.
For that, it would need global/external scope.


Bummer...thanks for the info.

AWOL

Sorry, that should have read "directly referenced".
You can, of course pass pointers and references between functions.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

AWOL

Another thing is it isn't necessary to set the pin mode of a pin you're going to do an analogRead on.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

michael_x


You can reference a variable, but not call it, unless the variable is a function pointer.

I think AWOL tries to fool you a bit ;) Of course you can use both functions and global variables:
One tab:
Code: [Select]
char msg[] = "Hello World";
void sayHello()
{
 Serial.println(msg);
}

Another tab:
Code: [Select]

extern void sayHello(); // NOT required
extern char msg[]; // required

void setup() {
}

void loop() {
 if (strlen(msg) > 2) sayHello();
 delay(2000);
}


Notice the "extern" keyword !
Arduino IDE tries to be smart and secretly adds the first line by itself.


Go Up