hey all, i have function1() that retrun some variable let say int A , and i would like to use it in a seconde function : function2() ;, how can i do that please ?
int function1() {
int A;
return A
}
void function2() {
function1();
Serial.print(A)
}
Thnaks
J-M-L
July 11, 2019, 4:53pm
2
The reference A
does not exist outside function1()
(read about variable scope )
You coud do:
void function2() {
Serial.print(function1());
}
or if you need the value for something else than printing, then create a local variable to store the result
void function2() {
int result = function1();
Serial.print(result);
if (result == 5) { ... }
}
Thank you but my function is mpore complicated it was just for explaining.
and i would to use the variabe in a condition or a boucle not just for printf ^^ , any suggestions ?
J-M-L
July 11, 2019, 4:56pm
4
was editing as you typed back - read again
how can i do that please ?
Change function2() to accept a parameter so that you can pass a value to it, then
//either
int aVariable = function1();
function2(aVariable);
//or
function2(function1());
int function1()
{
int A;
return A
}
void function2(int aParameter)
{
function1();
Serial.print(aParameter)
}
It’s still the same. Just imagine that your function call magically gets replaced by that return value.
So in an if statement:
if(function1() == 7)