diff options
author | dkf <donal.k.fellows@manchester.ac.uk> | 2004-10-27 14:24:37 (GMT) |
---|---|---|
committer | dkf <donal.k.fellows@manchester.ac.uk> | 2004-10-27 14:24:37 (GMT) |
commit | f2710bf0bb0c6ace5d1bc4f424b400537ffdb21c (patch) | |
tree | c19b22fbb2165682630fecbf3779e53b26c9002f /doc/return.n | |
parent | 4c2d0f20bfa9108949678cf49bfdc58eedc7bb93 (diff) | |
download | tcl-f2710bf0bb0c6ace5d1bc4f424b400537ffdb21c.zip tcl-f2710bf0bb0c6ace5d1bc4f424b400537ffdb21c.tar.gz tcl-f2710bf0bb0c6ace5d1bc4f424b400537ffdb21c.tar.bz2 |
More minor doc fixes
Diffstat (limited to 'doc/return.n')
-rw-r--r-- | doc/return.n | 106 |
1 files changed, 47 insertions, 59 deletions
diff --git a/doc/return.n b/doc/return.n index c1c09b7..e08dc05 100644 --- a/doc/return.n +++ b/doc/return.n @@ -6,7 +6,7 @@ '\" See the file "license.terms" for information on usage and redistribution '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. '\" -'\" RCS: @(#) $Id: return.n,v 1.10 2004/09/18 17:01:06 dkf Exp $ +'\" RCS: @(#) $Id: return.n,v 1.11 2004/10/27 14:24:37 dkf Exp $ '\" .so man.macros .TH return n 8.5 Tcl "Tcl Built-In Commands" @@ -37,7 +37,6 @@ evaluates the contents of a file as a script, an invocation of the \fBreturn\fR command will cause script evaluation to immediately cease, and the value \fIresult\fR (or an empty string) will be returned as the result of the \fBsource\fR command. - .SH "EXCEPTIONAL RETURN CODES" .PP In addition to the result of a procedure, the return @@ -89,7 +88,6 @@ files that are evaluated by the \fBsource\fR command. During the evaluation of the contents of a file as a script by \fBsource\fR, an invocation of the \fBreturn -code \fIcode\fR command will cause the return code of \fBsource\fR to be \fIcode\fR. - .SH "RETURN OPTIONS" .PP .VS 8.5 @@ -153,7 +151,6 @@ The value \fIoptions\fR must be a valid dictionary. The entries of that dictionary are treated as additional \fIoption value\fR pairs for the \fBreturn\fR command. .VE 8.5 - .SH "RETURN CODE HANDLING MECHANISMS" .PP Return codes are used in Tcl to control program flow. A Tcl script @@ -208,99 +205,90 @@ for the \fB-level\fR option (including the default value of 1) will cause the return code of the \fBreturn\fR command itself to be \fBTCL_RETURN\fR, triggering a return from the enclosing procedure. .VE 8.5 - .SH EXAMPLES - First, a simple example of using \fBreturn\fR to return from a procedure, interrupting the procedure body. - .CS proc printOneLine {} { - puts "line 1" ;# This line will be printed. - return - puts "line 2" ;# This line will not be printed. + puts "line 1" ;# This line will be printed. + \fBreturn\fR + puts "line 2" ;# This line will not be printed. } .CE - +.PP Next, an example of using \fBreturn\fR to set the value returned by the procedure. - .CS -proc returnX {} {return X} +proc returnX {} {\fBreturn\fR X} puts [returnX] ;# prints "X" .CE - +.PP Next, a more complete example, using \fBreturn -code error\fR to report invalid arguments. - .CS proc factorial {n} { - if {![string is integer $n] || ($n < 0)} { - return -code error \\ - "expected non-negative integer,\\ - but got \\"$n\\"" - } - if {$n < 2} { - return 1 - } - set m [expr {$n - 1}] - set code [catch {factorial $m} factor] - if {$code != 0} { - return -code $code $factor - } - set product [expr {$n * $factor}] - if {$product < 0} { - return -code error \\ - "overflow computing factorial of $n" - } - return $product + if {![string is integer $n] || ($n < 0)} { + \fBreturn\fR -code error \\ + "expected non-negative integer,\\ + but got \\"$n\\"" + } + if {$n < 2} { + \fBreturn\fR 1 + } + set m [expr {$n - 1}] + set code [catch {factorial $m} factor] + if {$code != 0} { + \fBreturn\fR -code $code $factor + } + set product [expr {$n * $factor}] + if {$product < 0} { + \fBreturn\fR -code error \\ + "overflow computing factorial of $n" + } + \fBreturn\fR $product } .CE - +.PP Next, a procedure replacement for \fBbreak\fR. - .CS proc myBreak {} { - return -code break + \fBreturn\fR -code break } .CE - +.PP .VS 8.5 With the \fB-level 0\fR option, \fBreturn\fR itself can serve as a replacement for \fBbreak\fR. - .CS -interp alias {} Break {} return -level 0 -code break +interp alias {} Break {} \fBreturn\fR -level 0 -code break .CE - +.PP An example of using \fBcatch\fR and \fBreturn -options\fR to re-raise a caught error: - .CS proc doSomething {} { - set resource [allocate] - catch { - # Long script of operations - # that might raise an error - } result options - deallocate $resource - return -options $options $result + set resource [allocate] + catch { + # Long script of operations + # that might raise an error + } result options + deallocate $resource + \fBreturn\fR -options $options $result } .CE - +.PP Finally an example of advanced use of the \fBreturn\fR options to create a procedure replacement for \fBreturn\fR itself: - .CS proc myReturn {args} { - set result "" - if {[llength $args] % 2} { - set result [lindex $args end] - set args [lrange $args 0 end-1] - } - set options [dict merge {-level 1} $args] - dict incr options -level - return -options $options $result + set result "" + if {[llength $args] % 2} { + set result [lindex $args end] + set args [lrange $args 0 end-1] + } + set options [dict merge {-level 1} $args] + dict incr options -level + \fBreturn\fR -options $options $result } .CE .VE 8.5 |