A Factorial Example - Newport XPS-Q8 Manual

Universal high-performance motion controller/driver
Table of Contents

Advertisement

XPS-Q8
EDH0307En1041 — 10/17
of many math functions supported by the
the procedure; it is defined only during execution of Diag. Variable scope is discussed
further in Section 7. It is not really necessary to use the variable c in this example. The
procedure can also be written as:
proc
Diag {a b} {
return [expr
}
The return command is used to return the result of the procedure. The return
command is optional in this example because the Tcl interpreter returns the value of
the last command in the body as the value of the procedure. So, the procedure could be
reduced to:
proc
Diag {a b} {
expr
sqrt($a * $a + $b * $b)
}
Note the use of curly braces in the example. The curly brace at the end of the first line
starts the third argument to proc, which is the command body. In this case, the Tcl
interpreter sees the opening left brace, causing it to ignore newline characters and scan
the text until a matching right brace is found. Double quotes have the same
property. They group characters, including
newlines, until another double quote is found. The result of the grouping is such that the
third argument to
embedded newlines will terminate each command.
The other crucial effect of placing the curly braces around the procedure body is to
delay any substitutions in the body, until the procedure is called. For example, the
variables a, b, and c are not defined until the procedure is called, so we do not want to
do variable substitution at the time Diag is defined.
proc
The
command supports additional features such as having a variable number of
arguments and default values for arguments.
2.2.8

A Factorial Example

To reinforce what we have learned thus far, see the example below which uses a
while
loop to compute the factorial function:
Example 1–13: A
proc
Factorial {x} {
set
i 1;
set
while
{$i <= $x} {
set
product
incr
i
}
return
$product
}
Factorial
10
⇒ 3628800
The semicolon used on the second line is there to remind you that it is a command
terminator, just like the newline character. The
numbers from one to the value of x. The first argument to
expression, and the second argument is the command body to be executed.
The same math expression evaluator used by the
to evaluate the boolean expression. There is no need to explicitly use the
command in the first argument to while, even if you have a much more complex
expression.
The loop body and the procedure body are grouped with curly braces in the same way.
The opening curly brace must be on the same line as
sqrt($a * $a + $b * $b)]
proc
is a sequence of commands. When they are evaluated later, the
while
loop to compute a factorial.
product 1
[expr
$product * $i]
8
expr
command. The variable c is local to
while
loop is used to multiply all
while
expr
command is used by
proc
and while. If you want to
Tcl Manual
is a boolean
while
expr

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents