Functions - Canon Camera Hackers Manual

Camera hackers manual berthold daum
Table of Contents

Advertisement

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:
99
5.4 Lua primer

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Powershot sx10 is

Table of Contents