What's new

Using the built-in exit function within user-defined functions

kotori

Senior Member
Hello everyone,

I thought I'd share a finding related to the exit function. It turns out that it's not always safe to use "exit" from within a function invoked using the "call" keyword. Since the effect of the exit function within this context is similar to the "return" keyword in many other languages I propose that you use the following function instead of using the exit function directly:
[pre]function return
declare x
x := x
exit
end function[/pre]
To illustrate the problem, calling the following function 2048 times renders the "call" keyword inoperable and any further function calls anywhere in the script are ignored:
[pre]function myfunc
if 1 # 0
exit
inc(x) { any variable }
end if
end function[/pre]
As a work-around change the code into:
[pre]function myfunc
if 1 # 0
return
inc(x) { any variable }
end if
end function[/pre]

Interestingly this function call stack limit of 2048 may limit the number of callbacks running in parallel in case your script invokes wait() from within a function called using multiple nested "call"s. Fortunately I guess it's not that common to invoke wait() at the most nested spots, i.e. even if you have a maximum function nesting level of 10 the limit is of no great concern if you only invoke wait() at nesting level 2.

Nils
 
Top Bottom