EasyManua.ls Logo

Canon Camera - Subroutines

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...
90 CHAPTER 5 Scripting
Of course, such a label needs to be defined—and this is the syntax:
restart:
print "script restarted"
While goto statements seem to be very convenient and flexible, they im-
pose dangers, too. GOTOs are considered harmful in professional program-
ming because programs tend to become incomprehensible and difficult to
maintain if there are many of them jumping all over the place. So, if you
can, use if ... then ... else ... endif, for ... next, or while ...
wend instead of goto. In a tiny script, however, I can’t see why a goto should
do any harm.
5.3.8 Subroutines
Subroutines have a very similar syntax as GOTOs, but they can simplify
programs instead of making them more difficult to read. A subroutine
definition starts with a label and ends with a return statement:
:waitdisp
do
print "Continue: DISP"
wait_click 3000
until is_pressed "display"
return
This subroutine prompts the user to press the DISP key and then waits until
this key is pressed. The advantage of defining such a piece of code in a
subroutine is that it can be called from any location within the script by
using the gosub statement:
gosub "waitdisp"
shoot
The return command within the subroutine will cause program control to
return to the line after the gosub command and execute the instruction
found there.
The CHDK uBasic implementation supports nested subroutines to a
depth of 10.

Table of Contents

Related product manuals