Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/return.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 12.0 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Contributions from Don Porter, NIST, 2003.  (not subject to US copyright)
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: return.n,v 1.19 2007/12/13 15:22:33 dgp Exp $
10'\"
11.so man.macros
12.TH return n 8.5 Tcl "Tcl Built-In Commands"
13.BS
14'\" Note:  do not modify the .SH NAME line immediately below!
15.SH NAME
16return \- Return from a procedure, or set return code of a script
17.SH SYNOPSIS
18\fBreturn \fR?\fIresult\fR?
19.sp
20\fBreturn \fR?\fB\-code \fIcode\fR? ?\fIresult\fR?
21.sp
22\fBreturn \fR?\fIoption value \fR...? ?\fIresult\fR?
23.BE
24.SH DESCRIPTION
25.PP
26In its simplest usage, the \fBreturn\fR command is used without options
27in the body of a procedure to immediately return control to the caller
28of the procedure.  If a \fIresult\fR argument is provided, its value
29becomes the result of the procedure passed back to the caller. 
30If \fIresult\fR is not specified then an empty string will be returned
31to the caller as the result of the procedure.
32.PP
33The \fBreturn\fR command serves a similar function within script
34files that are evaluated by the \fBsource\fR command.  When \fBsource\fR
35evaluates the contents of a file as a script, an invocation of
36the \fBreturn\fR command will cause script evaluation
37to immediately cease, and the value \fIresult\fR (or an empty string)
38will be returned as the result of the \fBsource\fR command.
39.SH "EXCEPTIONAL RETURN CODES"
40.PP
41In addition to the result of a procedure, the return
42code of a procedure may also be set by \fBreturn\fR
43through use of the \fB\-code\fR option.
44In the usual case where the \fB\-code\fR option is not
45specified the procedure will return normally.
46However, the \fB\-code\fR option may be used to generate an
47exceptional return from the procedure.
48\fICode\fR may have any of the following values:
49.TP 13
50\fBok (or 0)\fR
51Normal return:  same as if the option is omitted.  The return code
52of the procedure is 0 (\fBTCL_OK\fR).
53.TP 13
54\fBerror (1)\fR
55Error return: the return code of the procedure is 1 (\fBTCL_ERROR\fR).
56The procedure command behaves in its calling context as if it
57were the command \fBerror \fIresult\fR.  See below for additional
58options.
59.TP 13
60\fBreturn (2)\fR
61The return code of the procedure is 2 (\fBTCL_RETURN\fR).  The
62procedure command behaves in its calling context as if it
63were the command \fBreturn\fR (with no arguments).
64.TP 13
65\fBbreak (3)\fR
66The return code of the procedure is 3 (\fBTCL_BREAK\fR).  The
67procedure command behaves in its calling context as if it
68were the command \fBbreak\fR.
69.TP 13
70\fBcontinue (4)\fR
71The return code of the procedure is 4 (\fBTCL_CONTINUE\fR).  The
72procedure command behaves in its calling context as if it
73were the command \fBcontinue\fR.
74.TP 13
75\fIvalue\fR
76\fIValue\fR must be an integer;  it will be returned as the
77return code for the current procedure.
78.LP
79When a procedure wants to signal that it has received invalid
80arguments from its caller, it may use \fBreturn -code error\fR
81with \fIresult\fR set to a suitable error message.  Otherwise
82usage of the \fBreturn -code\fR option is mostly limited to
83procedures that implement a new control structure.
84.PP
85The \fBreturn \-code\fR command acts similarly within script
86files that are evaluated by the \fBsource\fR command.  During the
87evaluation of the contents of a file as a script by \fBsource\fR,
88an invocation of the \fBreturn \-code \fIcode\fR command will cause
89the return code of \fBsource\fR to be \fIcode\fR.
90.SH "RETURN OPTIONS"
91.PP
92.VS 8.5
93In addition to a result and a return code, evaluation of a command
94in Tcl also produces a dictionary of return options.  In general
95usage, all \fIoption value\fR pairs given as arguments to \fBreturn\fR
96become entries in the return options dictionary, and any values at all
97are acceptable except as noted below.  The \fBcatch\fR command may be
98used to capture all of this information \(em the return code, the result,
99and the return options dictionary \(em that arise from evaluation of a
100script.
101.VE 8.5
102.PP
103As documented above, the \fB\-code\fR entry in the return options dictionary
104receives special treatment by Tcl.  There are other return options also
105recognized and treated specially by Tcl.  They are:
106.TP
107\fB\-errorcode \fIlist\fR
108The \fB\-errorcode\fR option receives special treatment only when the value
109of the \fB\-code\fR option is \fBTCL_ERROR\fR.  Then the \fIlist\fR value
110is meant to be additional information about the error,
111presented as a Tcl list for further processing by programs.
112If no \fB\-errorcode\fR option is provided to \fBreturn\fR when
113the \fB\-code error\fR option is provided, Tcl will set the value
114of the \fB\-errorcode\fR entry in the return options dictionary
115to the default value of \fBNONE\fR.  The \fB\-errorcode\fR return
116option will also be stored in the global variable \fBerrorCode\fR.
117.TP
118\fB\-errorinfo \fIinfo\fR
119The \fB\-errorinfo\fR option receives special treatment only when the value
120of the \fB\-code\fR option is \fBTCL_ERROR\fR.  Then \fIinfo\fR is the initial
121stack trace, meant to provide to a human reader additional information
122about the context in which the error occurred.  The stack trace will
123also be stored in the global variable \fBerrorInfo\fR. 
124If no \fB\-errorinfo\fR option is provided to \fBreturn\fR when
125the \fB\-code error\fR option is provided, Tcl will provide its own
126initial stack trace value in the entry for \fB\-errorinfo\fR.  Tcl's
127initial stack trace will include only the call to the procedure, and
128stack unwinding will append information about higher stack levels, but
129there will be no information about the context of the error within
130the procedure.  Typically the \fIinfo\fR value is supplied from
131the value of \fB\-errorinfo\fR in a return options dictionary captured
132by the \fBcatch\fR command (or from the copy of that information
133stored in the global variable \fBerrorInfo\fR).
134.TP
135\fB\-level \fIlevel\fR
136.VS 8.5
137The \fB\-level\fR and \fB\-code\fR options work together to set the return
138code to be returned by one of the commands currently being evaluated.
139The \fIlevel\fR value must be a non-negative integer representing a number
140of levels on the call stack.  It defines the number of levels up the stack
141at which the return code of a command currently being evaluated should
142be \fIcode\fR.  If no \fB\-level\fR option is provided, the default value
143of \fIlevel\fR is 1, so that \fBreturn\fR sets the return code that the
144current procedure returns to its caller, 1 level up the call stack.  The
145mechanism by which these options work is described in more detail below.
146.VE 8.5
147.TP
148\fB\-options \fIoptions\fR
149.VS 8.5
150The value \fIoptions\fR must be a valid dictionary.  The entries of that
151dictionary are treated as additional \fIoption value\fR pairs for the
152\fBreturn\fR command.
153.VE 8.5
154.SH "RETURN CODE HANDLING MECHANISMS"
155.PP
156Return codes are used in Tcl to control program flow.  A Tcl script
157is a sequence of Tcl commands.  So long as each command evaluation
158returns a return code of \fBTCL_OK\fR, evaluation will continue to the next
159command in the script.  Any exceptional return code (non-\fBTCL_OK\fR)
160returned by a command evaluation causes the flow on to the next
161command to be interrupted.  Script evaluation ceases, and the
162exceptional return code from the command becomes the return code
163of the full script evaluation.  This is the mechanism by which
164errors during script evaluation cause an interruption and unwinding
165of the call stack.  It is also the mechanism by which commands
166like \fBbreak\fR, \fBcontinue\fR, and \fBreturn\fR cause script
167evaluation to terminate without evaluating all commands in sequence.
168.PP
169Some of Tcl's built-in commands evaluate scripts as part of their
170functioning.  These commands can make use of exceptional return
171codes to enable special features.  For example, the built-in
172Tcl commands that provide loops \(em such as \fBwhile\fR, \fBfor\fR,
173and \fBforeach\fR \(em evaluate a script that is the body of the
174loop.  If evaluation of the loop body returns the return code
175of \fBTCL_BREAK\fR or \fBTCL_CONTINUE\fR, the loop command can react in such
176a way as to give the \fBbreak\fR and \fBcontinue\fR commands
177their documented interpretation in loops.
178.PP
179.VS 8.5
180Procedure invocation also involves evaluation of a script, the body
181of the procedure.  Procedure invocation provides special treatment
182when evaluation of the procedure body returns the return code
183\fBTCL_RETURN\fR.  In that circumstance, the \fB\-level\fR entry in the
184return options dictionary is decremented.  If after decrementing,
185the value of the \fB\-level\fR entry is 0, then the value of
186the \fB\-code\fR entry becomes the return code of the procedure.
187If after decrementing, the value of the \fB\-level\fR entry is
188greater than zero, then the return code of the procedure is
189\fBTCL_RETURN\fR.  If the procedure invocation occurred during the
190evaluation of the body of another procedure, the process will
191repeat itself up the call stack, decrementing the value of the
192\fB\-level\fR entry at each level, so that the \fIcode\fR will
193be the return code of the current command \fIlevel\fR levels
194up the call stack.  The \fBsource\fR command performs the
195same handling of the \fBTCL_RETURN\fR return code, which explains
196the similarity of \fBreturn\fR invocation during a \fBsource\fR
197to \fBreturn\fR invocation within a procedure.
198.PP
199The return code of the \fBreturn\fR command itself triggers this
200special handling by procedure invocation.  If \fBreturn\fR
201is provided the option \fB\-level 0\fR, then the return code
202of the \fBreturn\fR command itself will be the value \fIcode\fR
203of the \fB\-code\fR option (or \fBTCL_OK\fR by default).  Any other value
204for the \fB\-level\fR option (including the default value of 1)
205will cause the return code of the \fBreturn\fR command itself
206to be \fBTCL_RETURN\fR, triggering a return from the enclosing procedure.
207.VE 8.5
208.SH EXAMPLES
209First, a simple example of using \fBreturn\fR to return from a
210procedure, interrupting the procedure body.
211.CS
212proc printOneLine {} {
213   puts "line 1"    ;# This line will be printed.
214   \fBreturn\fR         
215   puts "line 2"    ;# This line will not be printed.
216}
217.CE
218.PP
219Next, an example of using \fBreturn\fR to set the value
220returned by the procedure.
221.CS
222proc returnX {} {\fBreturn\fR X}
223puts [returnX]    ;# prints "X"
224.CE
225.PP
226Next, a more complete example, using \fBreturn -code error\fR
227to report invalid arguments.
228.CS
229proc factorial {n} {
230   if {![string is integer $n] || ($n < 0)} {
231      \fBreturn\fR -code error \e
232            "expected non-negative integer,\e
233             but got \e"$n\e""
234   }
235   if {$n < 2} {
236      \fBreturn\fR 1
237   }
238   set m [expr {$n - 1}]
239   set code [catch {factorial $m} factor]
240   if {$code != 0} {
241      \fBreturn\fR -code $code $factor
242   }
243   set product [expr {$n * $factor}]
244   if {$product < 0} {
245      \fBreturn\fR -code error \e
246            "overflow computing factorial of $n"
247   }
248   \fBreturn\fR $product
249}
250.CE
251.PP
252Next, a procedure replacement for \fBbreak\fR.
253.CS
254proc myBreak {} {
255   \fBreturn\fR -code break
256}
257.CE
258.PP
259.VS 8.5
260With the \fB\-level 0\fR option, \fBreturn\fR itself can serve
261as a replacement for \fBbreak\fR.
262.CS
263interp alias {} Break {} \fBreturn\fR -level 0 -code break
264.CE
265.PP
266An example of using \fBcatch\fR and \fBreturn -options\fR to
267re-raise a caught error:
268.CS
269proc doSomething {} {
270   set resource [allocate]
271   catch {
272      # Long script of operations
273      # that might raise an error
274   } result options
275   deallocate $resource
276   \fBreturn\fR -options $options $result
277}
278.CE
279.PP
280Finally an example of advanced use of the \fBreturn\fR options
281to create a procedure replacement for \fBreturn\fR itself:
282.CS
283proc myReturn {args} {
284   set result ""
285   if {[llength $args] % 2} {
286      set result [lindex $args end]
287      set args [lrange $args 0 end-1]
288   }
289   set options [dict merge {-level 1} $args]
290   dict incr options -level
291   \fBreturn\fR -options $options $result
292}
293.CE
294.VE 8.5
295.SH "SEE ALSO"
296break(n), catch(n), continue(n), dict(n), error(n), proc(n), source(n), tclvars(n)
297.SH KEYWORDS
298break, catch, continue, error, procedure, return
Note: See TracBrowser for help on using the repository browser.