proc procName argList code
All this actually does is add an entry to the table in the interpreter.
As an example:
proc incr {number increment} {
return [expr $number + $increment]
}
Now, you can do something like this:
puts [incr 10 1]
and get output like this:
11
ArgList is actually a list of two-element
lists, the the second element being a default value if the
argument isn't supplied.
We can make use of this for incr:
proc incr {number {increment 1}} {
return [expr $number + $increment]
}
so that increment defaults to one.
Now, we can call incr with only the
number argument:
puts [incr 10]
and get the same output:
11
argList is named
arg, then the procedure can be called with an
arbitrary number of arguments (for the last argument).
As an example, a procedure that runs an operator over all of
its arguments:
proc fold {identity operator args} {
set folded $identity
foreach number $args {
set folded [expr $folded $operator $number]
}
return $folded
}
and do stuff like:
puts [fold 0 + 1 2 5]
puts [fold 1 * 1 2 5]
and get this output:
8
10
source Command
source fileName
To use the autoloading feature, an index file of all the
library functions and which files they belong to is
created. Whenever the interpreter fails to find a
proc that has been called, it looks up this index
and sources the associated file for you.