1proc(n)                      Tcl Built-In Commands                     proc(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       proc - Create a Tcl procedure
9

SYNOPSIS

11       proc name args body
12_________________________________________________________________
13
14

DESCRIPTION

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.
29
30       When  name  is invoked a local variable will be created for each of the
31       formal arguments to the procedure; its value will be the value of  cor‐
32       responding  argument  in the invoking command or the argument's default
33       value.  Arguments with default values need not be specified in a proce‐
34       dure  invocation.   However,  there must be enough actual arguments for
35       all the formal arguments that don't have defaults, and there  must  not
36       be  any  extra  actual  arguments.  There is one special case to permit
37       procedures with variable numbers of  arguments.   If  the  last  formal
38       argument  has  the  name args, then a call to the procedure may contain
39       more actual arguments than the procedure has formals.   In  this  case,
40       all  of the actual arguments starting at the one that would be assigned
41       to args are combined into a list (as  if  the  list  command  had  been
42       used); this combined value is assigned to the local variable args.
43
44       When  body  is  being  executed, variable names normally refer to local
45       variables, which are created automatically when referenced and  deleted
46       when  the  procedure returns.  One local variable is automatically cre‐
47       ated for each of the procedure's arguments.  Global variables can  only
48       be  accessed  by  invoking  the  global  command  or the upvar command.
49       Namespace variables can only be accessed by invoking the variable  com‐
50       mand or the upvar command.
51
52       The proc command returns an empty string.  When a procedure is invoked,
53       the procedure's return value is the value specified in  a  return  com‐
54       mand.   If  the  procedure doesn't execute an explicit return, then its
55       return value is the value of the last command executed  in  the  proce‐
56       dure's  body.   If  an error occurs while executing the procedure body,
57       then the procedure-as-a-whole will return that same error.
58

EXAMPLES

60       This is a procedure that accepts arbitrarily many arguments and  prints
61       them out, one by one.
62              proc printArguments args {
63                 foreach arg $args {
64                    puts $arg
65                 }
66              }
67
68       This procedure is a bit like the incr command, except it multiplies the
69       contents of the named variable by the value, which defaults to 2:
70              proc mult {varName {multiplier 2}} {
71                 upvar 1 $varName var
72                 set var [expr {$var * $multiplier}]
73              }
74
75

SEE ALSO

77       info(n), unknown(n)
78
79

KEYWORDS

81       argument, procedure
82
83
84
85Tcl                                                                    proc(n)
Impressum