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

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 :slight_smile:
Thank you.

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:

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

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

1 Like

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

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.

2 Likes

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

2 Likes

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.

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.)