1return(n) Tcl Built-In Commands return(n)
2
3
4
5______________________________________________________________________________
6
8 return - Return from a procedure
9
11 return ?-code code? ?-errorinfo info? ?-errorcode code? ?string?
12_________________________________________________________________
13
14
16 Return immediately from the current procedure (or top-level command or
17 source command), with string as the return value. If string is not
18 specified then an empty string will be returned as result.
19
21 In addition to the result of a procedure, the return code of a proce‐
22 dure may also be set by return through use of the -code option. In the
23 usual case where the -code option isn't specified the procedure will
24 return normally. However, the -code option may be used to generate an
25 exceptional return from the procedure. Code may have any of the fol‐
26 lowing values:
27
28 ok (or 0) Normal return: same as if the option is omitted. The
29 return code of the procedure is 0 (TCL_OK).
30
31 error (1) Error return: the return code of the procedure is 1
32 (TCL_ERROR). The procedure command behaves in its calling
33 context as if it were the command error result. See below
34 for additional options.
35
36 return (2) The return code of the procedure is 2 (TCL_RETURN). The
37 procedure command behaves in its calling context as if it
38 were the command return (with no arguments).
39
40 break (3) The return code of the procedure is 3 (TCL_BREAK). The
41 procedure command behaves in its calling context as if it
42 were the command break.
43
44 continue (4) The return code of the procedure is 4 (TCL_CONTINUE). The
45 procedure command behaves in its calling context as if it
46 were the command continue.
47
48 value Value must be an integer; it will be returned as the
49 return code for the current procedure.
50
51 The -code option is rarely used. It is provided so that procedures
52 that implement new control structures can reflect exceptional condi‐
53 tions back to their callers.
54
55 Two additional options, -errorinfo and -errorcode, may be used to pro‐
56 vide additional information during error returns. These options are
57 ignored unless code is error.
58
59 The -errorinfo option specifies an initial stack trace for the error‐
60 Info variable; if it is not specified then the stack trace left in
61 errorInfo will include the call to the procedure and higher levels on
62 the stack but it will not include any information about the context of
63 the error within the procedure. Typically the info value is supplied
64 from the value left in errorInfo after a catch command trapped an error
65 within the procedure.
66
67 If the -errorcode option is specified then code provides a value for
68 the errorCode variable. If the option is not specified then errorCode
69 will default to NONE.
70
72 First, a simple example of using return to return from a procedure,
73 interrupting the procedure body.
74 proc printOneLine {} {
75 puts "line 1" ;# This line will be printed.
76 return
77 puts "line 2" ;# This line will not be printed.
78 }
79
80 Next, an example of using return to set the value returned by the pro‐
81 cedure.
82 proc returnX {} {return X}
83 puts [returnX] ;# prints "X"
84
85 Next, a more complete example, using return -code error to report
86 invalid arguments.
87 proc factorial {n} {
88 if {![string is integer $n] || ($n < 0)} {
89 return -code error \
90 "expected non-negative integer,\
91 but got \"$n\""
92 }
93 if {$n < 2} {
94 return 1
95 }
96 set m [expr {$n - 1}]
97 set code [catch {factorial $m} factor]
98 if {$code != 0} {
99 return -code $code $factor
100 }
101 set product [expr {$n * $factor}]
102 if {$product < 0} {
103 return -code error \
104 "overflow computing factorial of $n"
105 }
106 return $product
107 }
108
109 Next, a procedure replacement for break.
110 proc myBreak {} {
111 return -code break
112 }
113
114
116 break(n), catch(n), continue(n), error(n), proc(n), source(n),
117 tclvars(n)
118
119
121 break, catch, continue, error, procedure, return
122
123
124
125Tcl 7.0 return(n)