# Calling a function within a function

**URL:** <https://discourse.nodered.org/t/calling-a-function-within-a-function/10439>\
**Category:** General\
**Created:** [22 April 2019 20:46 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439 "2019-04-22T20:46:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ftricamo](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@ftricamo](https://discourse.nodered.org/u/ftricamo)\
**Post date:** [22 April 2019 20:46 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/1 "2019-04-22T20:46:29Z")

</div>

aside from bringing a msg into a function node and sending a msg out at the end of a function, how can i call a function from within a function?

in the middle of my function i need to perform some numerical computes that i can either copy and paste the same formulas throughout my functions or i can call a function.

I did write a function and saved it to a function library, but i don't see how to call it. (node.send?). I may need to call the same function 10 times within the function.

In my searches i see talk about 'sub flows'., but how is the 'return' implemented? is a sub flow wired or called?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 April 2019 21:14 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/2 "2019-04-22T21:14:45Z")

</div>

A subflow is wired the same as any node. The beauty of using a subflow is your function code exists only once (i.e. not an unmanageable 10 copy/paste affair).

Pass your data to input of subflow, get processed data out of its output(s)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 April 2019 21:20 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/3 "2019-04-22T21:20:25Z")

</div>

If you want to stay totally inside the one function you can just declare an internal function just like in any other piece of javascript. `var myfunc = function(foo) {...` style then call it from within your function.

Things to note are that the whole function is instantiated whenever a msg arrives so you if you need to maintain some context you must remember to do that.

---

<div class="post-metadata">

**Author:** ![ftricamo](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@ftricamo](https://discourse.nodered.org/u/ftricamo)\
**Post date:** [22 April 2019 22:23 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/4 "2019-04-22T22:23:32Z")

</div>

Thank you dceejay,

i'm not sure where i read it, but is it possible to declare a function as you stated as a 'global variable' where i change a memory config in settings.js?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [22 April 2019 22:36 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/5 "2019-04-22T22:36:37Z")

</div>

You can declare functions in global. But it isn't wise to do that within a flow, only from settings.js

That is because not all var handlers may support serialisation of functions.

But setting in the globals section of settings.js should always work. You can then use `global.get('myfnName') in a function node to get a reference to the function.

---

<div class="post-metadata">

**Author:** ![ftricamo](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@ftricamo](https://discourse.nodered.org/u/ftricamo)\
**Post date:** [22 April 2019 23:42 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/6 "2019-04-22T23:42:53Z")

</div>

do i simply add the function declaration at the end of the settings file?

```auto
function myFunction(n1,n2) {
  return n1 + n2;
}

```

```auto
var input = msg;
var off = input.payload.foff;   
var on = input.payload.fon;     
var count = 1;                  
var input_off_float = flow.get(["input_off_float"]);    
var ttltimeofffloatoff, ttltimeofffloaton, difference, calcdifference, testf ; 
var d = new Date();             
var timestamp1, timestamp2;     
var newfunction = global.get('myFunction');

timestamp1 = d.getTime();

if(input_off_float == 2){ 
    on=on+count; // increase count variable to write back to lifetime counter            
    timestamp2 = global.get(["offfloatlaststoptime"]); // get the last time off float went off to compute how long float has been off
    difference = timestamp1 - timestamp2; // computes the difference using epoch time
    calcdifference = parseInt(difference/1000);        
    flow.set(["difference"],difference); // sets difference to actual difference in milliseconds
    flow.set(["ttltimeofffloaton"],calcdifference); // gives us seconds for total time off float to on
    testf = newfunction(difference, 5);
    flow.set(["testf"],testf);
}
else if (input_off_float == 3){ 
    off=off+count; // increase count variable to write back to lifetime counter 
    timestamp2 = global.get(["onfloatlaststoptime"]); // get the global variable storing the last time the on float went to off status
    difference = timestamp1 - timestamp2; // computes the difference using epoch time
    calcdifference = parseInt(difference/1000);         
    flow.set(["difference"],difference);              
    flow.set(["ttltimetopumpfromhighfloat"],calcdifference); // set the total time var for the pump to empty  
    testf = newfunction(difference, 10);
    flow.set(["testf"],testf);
}

flow.set(["on"],on);
flow.set(["off"],off);

msg.payload.foff = off;
msg.payload.fon = on;

return msg;

```

this is my console output

```auto
22 Apr 17:40:10 - [error] [function:reads count file updates count writes back] TypeError: newfunction is not a function

```

I feel like i am close, but what am i missing?

thanks for your time...

---

<div class="post-metadata">

**Author:** ![ftricamo](https://avatars.discourse-cdn.com/v4/letter/f/e79b87/32.png) [@ftricamo](https://discourse.nodered.org/u/ftricamo)\
**Post date:** [23 April 2019 00:56 UTC](https://discourse.nodered.org/t/calling-a-function-within-a-function/10439/7 "2019-04-23T00:56:19Z")

</div>

This did it,

I can work with this

> [@Use function multiple times](https://discourse.nodered.org/t/use-function-multiple-times/7280/3):
>
> One approach would be to use the global.set() with the memory module. store your function in a global variable: global.set('sum', function(a, b) { return a + b; }, 'memoryOnly'); use your function: msg.payload = global.get('sum', 'memoryOnly')(2, 3); return msg; Note: you need this setting in the settings.js: contextStorage: { default: { module:"localfilesystem" }, memoryOnly: { module: "memory" } }, Flow: [{"id":"4ac…
