# return a variable of a function

**URL:** <https://forum.arduino.cc/t/return-a-variable-of-a-function/601177>\
**Category:** Programming\
**Created:** [July 11, 2019, 4:49pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177 "2019-07-11T16:49:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lgabed420](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@lgabed420](https://forum.arduino.cc/u/lgabed420)\
**Post date:** [July 11, 2019, 4:49pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/1 "2019-07-11T16:49:06Z")

</div>

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 ?

```cpp
int function1() {
int A;
return A

}

void function2() {
function1();
Serial.print(A)

}

```

Thnaks

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 11, 2019, 4:53pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/2 "2019-07-11T16:53:32Z")

</div>

The reference `A` does not exist outside `function1()` (read about [variable scope](https://www.arduino.cc/en/pmwiki.php?n=Reference/Scope))

You coud do:

```cpp
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

```cpp
void function2() {
  int result = function1();
  Serial.print(result);
  if (result == 5) { ... }
}

```

---

<div class="post-metadata">

**Author:** ![lgabed420](https://avatars.discourse-cdn.com/v4/letter/l/f07891/32.png) [@lgabed420](https://forum.arduino.cc/u/lgabed420)\
**Post date:** [July 11, 2019, 4:55pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/3 "2019-07-11T16:55:46Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 11, 2019, 4:56pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/4 "2019-07-11T16:56:51Z")

</div>

was editing as you typed back - read again

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [July 11, 2019, 4:57pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/5 "2019-07-11T16:57:36Z")

</div>

> how can i do that please ?

Change function2() to accept a parameter so that you can pass a value to it, then

```arduino
//either
int aVariable = function1();
function2(aVariable);

//or

function2(function1());

int function1()
{
  int A;
  return A
}

void function2(int aParameter)
{
  function1();
  Serial.print(aParameter)
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:55pm UTC](https://forum.arduino.cc/t/return-a-variable-of-a-function/601177/7 "2021-05-05T23:55:38Z")

</div>

Closed on May 5, 2021, 11:55pm UTC


