# Can a script have a function call another function ? See example

**URL:** <https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [May 24, 2025, 5:13am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656 "2025-05-24T05:13:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jack2](https://forum.inductiveautomation.com/u/Jack2)\
**Post date:** [May 24, 2025, 5:13am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/1 "2025-05-24T05:13:38Z")

</div>

Hi,  
I have a gateway timer script that has two functions and the second function calls the first function.  
I cannot tell why this is not working. Any help would be greatly appreciated.

Using a very simple example to help the discussion 🙂  
Thank you.

```python
logger = system.util.getLogger("ABCTest")

def add_1(x,y):
	return (x + y)
	
def add_2(a,b):
	c = add_1(a,b) # Call prev func
	return (c+a+b)

result_1 = add_1(5,5)
result_2 = add_2(5,5)

logger.info("Add_1 result " + str(result_1))
logger.info("Add_2 result " + str(result_2))

```

Error output:

```python
Traceback (most recent call last):
  File "<TimerScript:Test/Nested Functional Test @8,800ms >", line 11, in <module>
  File "<TimerScript:Test/Nested Functional Test @8,800ms >", line 7, in add_2
NameError: global name 'add_1' is not defined

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [May 24, 2025, 5:38am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/2 "2025-05-24T05:38:03Z")

</div>

> [@Jack2](#):
>
> `c = add(a,b)`

It looks like a typo on this line. Is this supposed to be `add_1`?

---

<div class="post-metadata">

**Author:** ![Jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jack2](https://forum.inductiveautomation.com/u/Jack2)\
**Post date:** [May 24, 2025, 6:32am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/4 "2025-05-24T06:32:00Z")

</div>

Sorry about that, copied the wrong code earlier. Corrected the original post with the same error.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [May 24, 2025, 7:13am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/5 "2025-05-24T07:13:35Z")

</div>

As Phil always say:  
Don't code in gateway events. Weird scoping rules.

Put your code in the script library, then call it from the gw event.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 24, 2025, 11:50pm UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/6 "2025-05-24T23:50:03Z")

</div>

If you've put a `def` in a gateway event, you've screwed up.

---

<div class="post-metadata">

**Author:** ![Jack2](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@Jack2](https://forum.inductiveautomation.com/u/Jack2)\
**Post date:** [May 25, 2025, 12:06am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/7 "2025-05-25T00:06:08Z")

</div>

Hi Phil,

Thanks for replying over the weekend.

Is it just due to scoping issue or are there other problems as well ? I have quite a few function definitions in gateway events and curious if it's worth changing them.

I'm having trouble finding documentation for script project library for the latest ignition. Any chance you have a link I could see to understand how it works ?  
Thanks.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 25, 2025, 12:50am UTC](https://forum.inductiveautomation.com/t/can-a-script-have-a-function-call-another-function-see-example/106656/8 "2025-05-25T00:50:03Z")

</div>

> [@Jack2](#):
>
> just due to scoping issue

This particular scoping issue is grievous.

It is absolutely worth changing them.

Or waiting for production-grade v8.3, which changes that legacy scope problem.

(I'd still change them. Making all events one-liners into the project library offers many advantages.)
