Debian Codegraph @

doc/proc.n

raw
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\"
5 '\" See the file "license.terms" for information on usage and redistribution
6 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 '\"
8 .TH proc n "" Tcl "Tcl Built-In Commands"
9 .so man.macros
10 .BS
11 '\" Note: do not modify the .SH NAME line immediately below!
12 .SH NAME
13 proc \- Create a Tcl procedure
14 .SH SYNOPSIS
15 \fBproc \fIname args body\fR
16 .BE
17 .SH DESCRIPTION
18 .PP
19 The \fBproc\fR command creates a new Tcl procedure named
20 \fIname\fR, replacing
21 any existing command or procedure there may have been by that name.
22 Whenever the new command is invoked, the contents of \fIbody\fR will
23 be executed by the Tcl interpreter.
24 Normally, \fIname\fR is unqualified
25 (does not include the names of any containing namespaces),
26 and the new procedure is created in the current namespace.
27 If \fIname\fR includes any namespace qualifiers,
28 the procedure is created in the specified namespace.
29 \fIArgs\fR specifies the formal arguments to the
30 procedure. It consists of a list, possibly empty, each of whose
31 elements specifies
32 one argument. Each argument specifier is also a list with either
33 one or two fields. If there is only a single field in the specifier
34 then it is the name of the argument; if there are two fields, then
35 the first is the argument name and the second is its default value.
36 Arguments with default values that are followed by non-defaulted
37 arguments become required arguments; enough actual arguments must be
38 supplied to allow all arguments up to and including the last required
39 formal argument.
40 .PP
41 When \fIname\fR is invoked a local variable
42 will be created for each of the formal arguments to the procedure; its
43 value will be the value of corresponding argument in the invoking command
44 or the argument's default value.
45 Actual arguments are assigned to formal arguments strictly in order.
46 Arguments with default values need not be
47 specified in a procedure invocation. However, there must be enough
48 actual arguments for all the
49 formal arguments that do not have defaults, and there must not be any extra
50 actual arguments.
51 Arguments with default values that are followed by non-defaulted
52 arguments become de-facto required arguments, though this may change
53 in a future version of Tcl; portable code should ensure that all
54 optional arguments come after all required arguments.
55 .PP
56 There is one special case to permit procedures with
57 variable numbers of arguments. If the last formal argument has the name
58 .QW \fBargs\fR ,
59 then a call to the procedure may contain more actual arguments
60 than the procedure has formal arguments. In this case, all of the actual arguments
61 starting at the one that would be assigned to \fBargs\fR are combined into
62 a list (as if the \fBlist\fR command had been used); this combined value
63 is assigned to the local variable \fBargs\fR.
64 .PP
65 When \fIbody\fR is being executed, variable names normally refer to
66 local variables, which are created automatically when referenced and
67 deleted when the procedure returns. One local variable is automatically
68 created for each of the procedure's arguments.
69 Other variables can only be accessed by invoking one of the \fBglobal\fR,
70 \fBvariable\fR, \fBupvar\fR or \fBnamespace upvar\fR commands.
71 The current namespace when \fIbody\fR is executed will be the
72 namespace that the procedure's name exists in, which will be the
73 namespace that it was created in unless it has been changed with
74 \fBrename\fR.
75 '\" We may change this! It makes [variable] unstable when renamed and is
76 '\" frankly pretty crazy, but doing it right is harder than it looks.
77 .PP
78 The \fBproc\fR command returns an empty string. When a procedure is
79 invoked, the procedure's return value is the value specified in a
80 \fBreturn\fR command. If the procedure does not execute an explicit
81 \fBreturn\fR, then its return value is the value of the last command
82 executed in the procedure's body.
83 If an error occurs while executing the procedure
84 body, then the procedure-as-a-whole will return that same error.
85 .SH EXAMPLES
86 .PP
87 This is a procedure that takes two arguments and prints both their sum
88 and their product. It also returns the string
89 .QW OK
90 to the caller as an explicit result.
91 .PP
92 .CS
93 \fBproc\fR printSumProduct {x y} {
94 set sum [expr {$x + $y}]
95 set prod [expr {$x * $y}]
96 puts "sum is $sum, product is $prod"
97 return "OK"
98 }
99 .CE
100 .PP
101 This is a procedure that accepts arbitrarily many arguments and prints
102 them out, one by one.
103 .PP
104 .CS
105 \fBproc\fR printArguments args {
106 foreach arg $args {
107 puts $arg
108 }
109 }
110 .CE
111 .PP
112 This procedure is a bit like the \fBincr\fR command, except it
113 multiplies the contents of the named variable by the value, which
114 defaults to \fB2\fR:
115 .PP
116 .CS
117 \fBproc\fR mult {varName {multiplier 2}} {
118 upvar 1 $varName var
119 set var [expr {$var * $multiplier}]
120 }
121 .CE
122 .SH "SEE ALSO"
123 info(n), unknown(n)
124 .SH KEYWORDS
125 argument, procedure
126 '\" Local Variables:
127 '\" mode: nroff
128 '\" End: