# Reference function within another function node

**URL:** <https://discourse.nodered.org/t/reference-function-within-another-function-node/60870>\
**Category:** General\
**Tags:** function-node\
**Created:** [6 April 2022 10:07 UTC](https://discourse.nodered.org/t/reference-function-within-another-function-node/60870 "2022-04-06T10:07:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashfaak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ashfaak/32/34409_2.png) [@Ashfaak](https://discourse.nodered.org/u/Ashfaak)\
**Post date:** [6 April 2022 10:07 UTC](https://discourse.nodered.org/t/reference-function-within-another-function-node/60870/1 "2022-04-06T10:07:11Z")

</div>

Hi

Consider 2 function nodes, A and B.  
Is it possible to reference a function written in A from node B?

I've tried searching for "referencing functions" and "scope of functions in function node".

Example:

```auto
[
    {
        "id": "95c7830baecc1f89",
        "type": "function",
        "z": "558ac96c.c926d8",
        "name": "A: Spreadsheet numbering functions",
        "func": "//Use these to convert to and from spreadsheet column numbering\n//https://codereview.stackexchange.com/questions/16124/implement-numbering-scheme-like-a-b-c-aa-ab-aaa-similar-to-converting?rq=1\nfunction convertToNumberingScheme(number) {\n var letters = \"\";\n const charCodeOfA = \"A\".charCodeAt(0);\n\n do {\n number -= 1;\n letters = String.fromCharCode(charCodeOfA + (number % 26)) + letters;\n number = (number / 26) >> 0; // quick `floor`\n } while(number > 0);\n\n return letters;\n}\n\n//https://stackoverflow.com/questions/9905533/convert-excel-column-alphabet-e-g-aa-to-number-e-g-25\nfunction lettersToNumber(letters){\n return letters.split('').reduce((r, a) => r * 26 + parseInt(a, 36) - 9, 0);\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2490,
        "y": 1060,
        "wires": [
            []
        ]
    },
    {
        "id": "3ad0b91b0c6272be",
        "type": "inject",
        "z": "558ac96c.c926d8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 2360,
        "y": 1120,
        "wires": [
            [
                "55717a09a50eba9a"
            ]
        ]
    },
    {
        "id": "1b48414330830eff",
        "type": "debug",
        "z": "558ac96c.c926d8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 2630,
        "y": 1120,
        "wires": []
    },
    {
        "id": "55717a09a50eba9a",
        "type": "function",
        "z": "558ac96c.c926d8",
        "name": "B: Test",
        "func": "const radix = 26;\nvar radixTest = [];\n\nfor(var i=1; i <182; i++){\n radixTest[i] = {};\n radixTest[i].string = i.toString(radix);\n radixTest[i].num = parseInt(radixTest[i].string, radix);\n radixTest[i].convertToNumberingScheme = convertToNumberingScheme(i);\n radixTest[i].lettersToNumber = lettersToNumber(radixTest[i].convertToNumberingScheme)\n}\n\nreturn {radixTest:radixTest};\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2500,
        "y": 1120,
        "wires": [
            [
                "1b48414330830eff"
            ]
        ]
    }
]

```

I feel like I've seen this in some node's example, but I cannot remember which one.

Thanks,  
Ashfaak

---

<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:** [6 April 2022 11:02 UTC](https://discourse.nodered.org/t/reference-function-within-another-function-node/60870/2 "2022-04-06T11:02:47Z")

</div>

> [@Ashfaak](#):
>
> Is it possible to reference a function written in A from node B?

Yes, but it is not very "node-red". You lose the graphical wiring/traceability aspect. A more "low-code" solution is to use link-call nodes (or sub flows) & put your utility functions in separate flows then re-use those.

However, if you already have a utility function & simply want to share it without copy/paste, then add it to an object and store it in either flow or global context.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40d66314374bc3b3956e9c6004d3410a6123b402.png)

then in function `B`, you can grab it and use it...

```auto
//get convertToNumberingScheme from flow.util
const { convertToNumberingScheme, lettersToNumber} = flow.get("util");
let x1 = convertToNumberingScheme("x1");
let x = lettersToNumber("x");

```

* * *

##### working demo flow

```auto
[
    {
        "id": "95c7830baecc1f89",
        "type": "function",
        "z": "4c5ad8c7caa80822",
        "name": "A: Spreadsheet numbering functions",
        "func": "//Use these to convert to and from spreadsheet column numbering\n//https://codereview.stackexchange.com/questions/16124/implement-numbering-scheme-like-a-b-c-aa-ab-aaa-similar-to-converting?rq=1\nfunction convertToNumberingScheme(number) {\n let letters = \"\";\n const charCodeOfA = \"A\".charCodeAt(0);\n\n do {\n number -= 1;\n letters = String.fromCharCode(charCodeOfA + (number % 26)) + letters;\n number = (number / 26) >> 0; // quick `floor`\n } while(number > 0);\n\n return letters;\n}\n\n//https://stackoverflow.com/questions/9905533/convert-excel-column-alphabet-e-g-aa-to-number-e-g-25\nfunction lettersToNumber(letters){\n return letters.split('').reduce((r, a) => r * 26 + parseInt(a, 36) - 9, 0);\n}\n\n\nmsg.payload = {\n convertToNumberingScheme,\n lettersToNumber\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1718,
        "y": 192,
        "wires": [
            [
                "32df1e7b64c344c2"
            ]
        ]
    },
    {
        "id": "3ad0b91b0c6272be",
        "type": "inject",
        "z": "4c5ad8c7caa80822",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1588,
        "y": 252,
        "wires": [
            [
                "55717a09a50eba9a"
            ]
        ]
    },
    {
        "id": "1b48414330830eff",
        "type": "debug",
        "z": "4c5ad8c7caa80822",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1858,
        "y": 252,
        "wires": []
    },
    {
        "id": "55717a09a50eba9a",
        "type": "function",
        "z": "4c5ad8c7caa80822",
        "name": "B: Test",
        "func": "const radix = 26;\nconst radixTest = [];\n\n//get convertToNumberingScheme from flow.util\nconst { convertToNumberingScheme, lettersToNumber } = flow.get(\"util\");\n\nfor (let i = 1; i < 182; i++) {\n radixTest[i] = {};\n radixTest[i].string = i.toString(radix);\n radixTest[i].num = parseInt(radixTest[i].string, radix);\n radixTest[i].convertToNumberingScheme = convertToNumberingScheme(i);\n radixTest[i].lettersToNumber = lettersToNumber(radixTest[i].convertToNumberingScheme)\n}\n\nreturn { radixTest: radixTest };\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1728,
        "y": 252,
        "wires": [
            [
                "1b48414330830eff"
            ]
        ]
    },
    {
        "id": "05745c60136cfcab",
        "type": "inject",
        "z": "4c5ad8c7caa80822",
        "name": "init",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1478,
        "y": 192,
        "wires": [
            [
                "95c7830baecc1f89"
            ]
        ]
    },
    {
        "id": "32df1e7b64c344c2",
        "type": "change",
        "z": "4c5ad8c7caa80822",
        "name": "store functions in flow.util",
        "rules": [
            {
                "t": "set",
                "p": "util",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2018,
        "y": 192,
        "wires": [
            []
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Ashfaak](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ashfaak/32/34409_2.png) [@Ashfaak](https://discourse.nodered.org/u/Ashfaak)\
**Post date:** [6 April 2022 11:32 UTC](https://discourse.nodered.org/t/reference-function-within-another-function-node/60870/3 "2022-04-06T11:32:42Z")

</div>

Thank you Steve-Mcl!

---

<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:** [20 April 2022 11:33 UTC](https://discourse.nodered.org/t/reference-function-within-another-function-node/60870/4 "2022-04-20T11:33:22Z")

</div>

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