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
:
Summary of Contents for Camera
Page 1: ......
Page 2: ...The Canon Camera Hackers Manual ...
Page 3: ......
Page 4: ...Berthold Daum The Canon Camera Hackers Manual Teach Your Camera New Tricks ...
Page 19: ...10 CH APTER 2 Cameras and Operating Systems ...
Page 25: ...16 CH APTER 3 ...
Page 85: ...76 CH APTER 4 Teach Your Camera New Tricks ...
Page 213: ...204 CH APTER 6 ...
Page 253: ...244 AP PENDIX ...