<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Expressions > Function Calls |
There are three variants of functions in MasterOPC:
1.System functions and functions of server API
2.User functions in current script (that is, functions within one tag, device or sub-device)
3.User functions in external script with respect to current script (for example, invocation of a function,which is defined in a device script, in a tag script).
Let's consider calls of system functions and user functions of current scripts first. In this case, a function call has the following syntax:
prefixexp(args)
A function can return several values or none of values. Examples of function calls:
a=f(x) --function is called, 1 argument is passed, and 1 parameter is returned
a,b,c,d=f(x,2,"string") --function is called, 3 arguments are passed, and 4 values are returned
f(x) --function is called, there no values returned
f() --function is called, there no arguments passed, and there are no values returned
All argument expressions are evaluated before the call. A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f’string’ (or f"string" or f[[string]]) is syntactic sugar for f(’string’); that is, the argument list is a single literal string.
As an exception to the free-format syntax of Lua, you cannot put a line break before the ’(’ in a function call. This restriction avoids some ambiguities in the language. If you write
a = f
(g).x(a)
Lua would see that as a single statement, a = f(g).x(a). So, if you want two statements, you must add a semi-colon between them. If you actually want to call f, you must remove the line break before (g).
A call of the form return functioncall is called a tail call. Lua implements proper tail calls (or proper tail recursion): in a tail call, the called function reuses the stack entry of the calling function. Therefore, there is no limit on the number of nested tail calls that a program can execute. However, a tail call erases any debug information about the calling function. Note that a tail call only happens with a particular syntax, where the return has one single function call as argument; this syntax makes the calling function return exactly the returns of the called function. So, none of the following examples are tail calls:
return (f(x)) -- results adjusted to 1
return 2 * f(x) --
return x, f(x) -- additional results
f(x); return -- results discarded
return x or f(x) -- results adjusted to 1
Invocation of external functions
External functions are functions defined in other scripts (for example, a function defined in a device script is external with respect to a tag script). This method is convenient if a function is called repeatedly - for example, a function, which calculates a check sum, is defined in a device script, and is called from a script of each tag when polling the device.
You cannot use standard means in order to call external functions, because functions with the same name can be created in different configuration elements. To solve this problem, special functions are added to API - see Function Invocation for details.