1proc(n) Tcl Built-In Commands proc(n)
2
3
4
5______________________________________________________________________________
6
8 proc - Create a Tcl procedure
9
11 proc name args body
12_________________________________________________________________
13
14
16 The proc command creates a new Tcl procedure named name, replacing any
17 existing command or procedure there may have been by that name. When‐
18 ever the new command is invoked, the contents of body will be executed
19 by the Tcl interpreter. Normally, name is unqualified (does not
20 include the names of any containing namespaces), and the new procedure
21 is created in the current namespace. If name includes any namespace
22 qualifiers, the procedure is created in the specified namespace. Args
23 specifies the formal arguments to the procedure. It consists of a
24 list, possibly empty, each of whose elements specifies one argument.
25 Each argument specifier is also a list with either one or two fields.
26 If there is only a single field in the specifier then it is the name of
27 the argument; if there are two fields, then the first is the argument
28 name and the second is its default value. Arguments with default val‐
29 ues that are followed by non-defaulted arguments become required argu‐
30 ments. In 8.6 this will be considered an error.
31
32 When name is invoked a local variable will be created for each of the
33 formal arguments to the procedure; its value will be the value of cor‐
34 responding argument in the invoking command or the argument's default
35 value. Actual arguments are assigned to formal arguments strictly in
36 order. Arguments with default values need not be specified in a proce‐
37 dure invocation. However, there must be enough actual arguments for
38 all the formal arguments that do not have defaults, and there must not
39 be any extra actual arguments. Arguments with default values that are
40 followed by non-defaulted arguments become required arguments (in 8.6
41 it will be considered an error). There is one special case to permit
42 procedures with variable numbers of arguments. If the last formal
43 argument has the name args, then a call to the procedure may contain
44 more actual arguments than the procedure has formals. In this case,
45 all of the actual arguments starting at the one that would be assigned
46 to args are combined into a list (as if the list command had been
47 used); this combined value is assigned to the local variable args.
48
49 When body is being executed, variable names normally refer to local
50 variables, which are created automatically when referenced and deleted
51 when the procedure returns. One local variable is automatically cre‐
52 ated for each of the procedure's arguments. Other variables can only
53 be accessed by invoking one of the global, variable, upvar or namespace
54 upvar commands.
55
56 The proc command returns an empty string. When a procedure is invoked,
57 the procedure's return value is the value specified in a return com‐
58 mand. If the procedure does not execute an explicit return, then its
59 return value is the value of the last command executed in the proce‐
60 dure's body. If an error occurs while executing the procedure body,
61 then the procedure-as-a-whole will return that same error.
62
64 This is a procedure that accepts arbitrarily many arguments and prints
65 them out, one by one.
66 proc printArguments args {
67 foreach arg $args {
68 puts $arg
69 }
70 }
71
72 This procedure is a bit like the incr command, except it multiplies the
73 contents of the named variable by the value, which defaults to 2:
74 proc mult {varName {multiplier 2}} {
75 upvar 1 $varName var
76 set var [expr {$var * $multiplier}]
77 }
78
79
81 info(n), unknown(n)
82
83
85 argument, procedure
86
87
88
89Tcl proc(n)