# How to divide two variables defined in 2 functions

**URL:** <https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036>\
**Category:** General\
**Created:** [17 June 2022 02:05 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036 "2022-06-17T02:05:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruno1](https://avatars.discourse-cdn.com/v4/letter/b/b2d939/32.png) [@Bruno1](https://discourse.nodered.org/u/Bruno1)\
**Post date:** [17 June 2022 02:05 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036/1 "2022-06-17T02:05:38Z")

</div>

Hi everyone!  
In this case i cant´t calculate the division of two variables defined in diferent functions. I want that my output will be a numeric (x/y).

How to resolve this problem?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/5/852a55cd6f3cfbb009474cbe6ff5d1a5dd774bbb.png)

In the first function (top left) i defined de variable x and the down i define the variable y.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/b/4bc173258fb32b76254dec77b68be18a5327ab0e.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48bcb65d190dae8c93d41aa49017798b0063fca2.png)

But in the respective debugs appeears:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/990acfe8a2237b30cbe93d92671ea92c5e82b329.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c0ec391bc89b4193e51fab6a9fa70ca3e878c50.png)

After that, i wont make a function that divide x/y and in last function i put this code:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35475f8927b2f68154748ef1eaf4aa34cbc33428.png)

And the output is:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32e4bf57f75c53e28f1aa25b73973737b758a143.png)

So, what you sugest for i can divide x for y?

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [17 June 2022 02:11 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036/2 "2022-06-17T02:11:32Z")

</div>

Messages won't show up at the same time so you will need to store the messages until you have both messages. Then you can retrieve them to do your math . There are several ways to do this Google flow.set or context.set for two ways and I'm sure someone will chime in with others.

---

<div class="post-metadata">

**Author:** ![MaccYYcode](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maccyycode/32/63673_2.png) [@MaccYYcode](https://discourse.nodered.org/u/MaccYYcode)\
**Post date:** [23 June 2022 05:15 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036/3 "2022-06-23T05:15:24Z")

</div>

Use global.set("x",x) to store the variable and use global.get("x") to get it

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [23 June 2022 08:49 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036/4 "2022-06-23T08:49:22Z")

</div>

You also have to think about the case where two msgs go thru one function before a msg goes thru the second function. That could cause an incorrect result. You might be able to use a `join` node but not knowing what the flow prior to the two function nodes makes it hard to say.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 August 2022 08:49 UTC](https://discourse.nodered.org/t/how-to-divide-two-variables-defined-in-2-functions/64036/5 "2022-08-22T08:49:46Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
