Models 707B and 708B Switching Matrix Reference Manual
Section 6: Instrument programming
707B-901-01 Rev. A / August 2010
6-19
Functions
Lua makes it simple to group commands and statements using the
function
keyword. Functions
can take zero, one, or multiple parameters, and they return zero, one, or multiple parameters.
Functions can be used to form expressions that calculate and return a value; they also can act as
statements that execute specific tasks.
Functions are first-class values in Lua. That means that functions can be stored in variables, passed
as arguments to other functions, and returned as results. They can also be stored in tables.
Note that when a function is defined, it is a global variable in the runtime environment. Like all global
variables, the functions persist until they are removed from the runtime environment, are overwritten,
or the instrument is turned off.
Create functions using the function keyword
Functions are created with a message or Lua code in the form:
myFunction = function (parameterX) functionBody end
Where:
•
myFunction
: The name of the function
•
parameterX
: Parameter values. You can have multiple parameters. Change the value defined
by
X
for each parameter and use a comma to separate the values.
•
functionBody
is the code that is executed when the function is called
To execute a function, substitute appropriate values for
parameterX
and insert them into a message
formatted as:
myFunction(valueForParameterX, valueForParameterY)
Where
valueForParameterX
and
valueForParameterY
represent the values to be passed to
the function call for the given parameters.
Example 1
Code Notes
and
output
function add_two(parameter1, parameter2)
return para parameter2
end
Creates a variable named
add_two
that has a
variable type of function.
print(add_two(3, 4))
7.00000