EasyManua.ls Logo

Canon Camera - Functions

Canon Camera
257 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
5.4 Lua primer 99
5.4.9 Functions
Instead of subroutines as in uBasic, Lua features more versatile functions.
Each function consists of a function header, with a function name and
parameters, and a function body. The whole function definition is enclosed
by the pair function ... end. The list of parameters is enclosed in paren-
theses and separated by commas. Even parameterless functions use these
parentheses with—aka—an empty list of parameters. For example:
function waitdisp()
repeat
print("Continue: DISP")
wait_click(3000)
until is_pressed("display")
end
In contrast to uBasic, functions can return one or several values. This is
done with the return command followed by a single return value or by a
comma-separated list of return values. The following function returns the
time difference between the function start and a click on the specified
button in milliseconds:
function stopwatch(k)
t = get_tick_count()
repeat
wait_click(3000)
until is_pressed(k)
d = get_tick_count()-t
return d
end
A special statement to invoke a function (like the gosub in uBasic) is not
necessary—it is sufficient to simply invoke the function by its name, fol-
lowed by the list of parameters enclosed in parentheses. For example:
d = stopwatch("display")
print(d)
A very powerful feature is the ability to assign a function definition to a
variable. The function definition can then be passed as a parameter to
other functions, stored in a table, or even returned as a result of another
function. The following code assigns an anonymous function to the vari-
able hello:

Table of Contents

Related product manuals