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