12 An Overview of Tcl and Tk
DRAFT (8/12/93): Distribution Restricted
plies the result value by base and then decrements p. When p reaches zero the result con-
tains the desired power of base.
The return command causes the procedure to exit with the value of variable
result as the procedure’s result. If it is omitted then the return value of the procedure
will be the result of the last command in the procedure’s body. In the case of power this
would be the result of while, which is always an empty string.
The use of braces in this example is crucial. The single most difficult issue in writing
Tcl scripts is managing substitutions: making them happen when you want them and pre-
venting them from happening when you don’t want them. Braces prevent substitutions or
defer them until later. The body of the procedure must be enclosed in braces because we
don’t want variable and command substitutions to occur at the time the body is passed to
proc as an argument; we want the substitutions to occur later, when the body is evaluated
as a Tcl script. The body of the while command is enclosed in braces for the same rea-
son: rather than performing the substitutions once, while parsing the while command,
we want the substitutions to be performed over and over, each time the body is evaluated.
Braces are also needed in the “{$p > 0}” argument to while. Without them the value
of variable p would be substituted when parsing the while command; the expression
would have a constant value and while would loop forever (you can try replacing some
of the braces in the example with double quotes to see what happens).
In the examples in this book I use a stylized syntax where the open brace for an argu-
ment that is a Tcl script appears at the end of one line, the script follows on successive
lines indented, and the close brace is on a line by itself after the script. Although I think
that this makes for readable scripts, Tcl doesn’t require this particular syntax. Script argu-
ments are subject to the same syntax rules as any other arguments; in fact the Tcl inter-
preter doesn’t even know that an argument is a script at the time it parses it. One
consequence of this is that the open parenthesis must be on the same line as the preceding
portion of the command. If the open brace is moved to a line by itself then the newline
before the open brace will terminate the command.
By now you have seen nearly the entire Tcl language syntax. The only remaining syn-
tactic feature is backslash substitution, which allows you to enter special characters such
as dollar-signs into a word without enclosing the entire word in braces. Note that while
and proc are not special syntactic elements in Tcl. They are just commands that take
arguments just like all Tcl commands. The only special thing about while and proc is
that they treat some of their arguments as Tcl scripts and cause the scripts to be evaluated.
Many other commands also do this. The button command was one example (its -com-
mand option is a Tcl script), and you’ll read about several other control structures later on,
such as for, foreach, case, and eval.
One final note about procedures. The variables in a procedure are normally local to
that procedure and will not be visible outside the procedure. In the power example the
local variables include the arguments base and p as well as the variable result. A
fresh set of local variables is created for each call to a procedure (arguments are passed by
copying their values), and when a procedure returns its local variables are deleted. Vari-