Artifact cc7cf6553e7e2d2fc6f4d1c36794a09c80466545ef4cc59592148d090fa1825e:
- File doc/markdown/return.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 12891) [annotate] [blame] [check-ins using]
CommandName: return ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- break(n)
- catch(n)
- continue(n)
- dict(n)
- error(n)
- errorCode(n)
- errorInfo(n)
- proc(n)
- source(n)
- throw(n)
- try(n) Keywords:
- break
- catch
- continue
- error
- exception
- procedure
- result
- return Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-1996 Sun Microsystems, Inc. ---
Name
return - Return from a procedure, or set return code of a script
Synopsis
::: {.synopsis} ::: [return]{.cmd} [result]{.optarg} [return]{.cmd} [[-code]{.lit} [code]{.arg}]{.optarg} [result]{.optarg} [return]{.cmd} [option value= ...? ?+result]{.optarg} :::
Description
In its simplest usage, the fBreturnfR command is used without options in the body of a procedure to immediately return control to the caller of the procedure. If a fIresultfR argument is provided, its value becomes the result of the procedure passed back to the caller. If fIresultfR is not specified then an empty string will be returned to the caller as the result of the procedure.
The fBreturnfR command serves a similar function within script files that are evaluated by the fBsourcefR command. When fBsourcefR evaluates the contents of a file as a script, an invocation of the fBreturnfR command will cause script evaluation to immediately cease, and the value fIresultfR (or an empty string) will be returned as the result of the fBsourcefR command.
Exceptional return codes
In addition to the result of a procedure, the return code of a procedure may also be set by fBreturnfR through use of the fB-codefR option. In the usual case where the fB-codefR option is not specified the procedure will return normally. However, the fB-codefR option may be used to generate an exceptional return from the procedure. fICodefR may have any of the following values:
ok (or fB0fR) : Normal return: same as if the option is omitted. The return code of the procedure is 0 (fBTCL_OKfR).
error (or fB1fR) : Error return: the return code of the procedure is 1 (fBTCL_ERRORfR). The procedure command behaves in its calling context as if it were the command fBerrorfR fIresultfR. See below for additional options.
return (or fB2fR) : The return code of the procedure is 2 (fBTCL_RETURNfR). The procedure command behaves in its calling context as if it were the command fBreturnfR (with no arguments).
break (or fB3fR) : The return code of the procedure is 3 (fBTCL_BREAKfR). The procedure command behaves in its calling context as if it were the command fBbreakfR.
continue (or fB4fR) : The return code of the procedure is 4 (fBTCL_CONTINUEfR). The procedure command behaves in its calling context as if it were the command fBcontinuefR.
value : Value must be an integer; it will be returned as the return code for the current procedure. Applications and packages should use values in the range 5 to 1073741823 (0x3fffffff) for their own purposes. Values outside this range are reserved for use by Tcl. .LP When a procedure wants to signal that it has received invalid arguments from its caller, it may use fBreturn -code errorfR with fIresultfR set to a suitable error message. Otherwise usage of the fBreturn -codefR option is mostly limited to procedures that implement a new control structure.
The fBreturn -codefR command acts similarly within script files that are evaluated by the fBsourcefR command. During the evaluation of the contents of a file as a script by fBsourcefR, an invocation of the fBreturn -codefR fIcodefR command will cause the return code of fBsourcefR to be fIcodefR.
Return options
In addition to a result and a return code, evaluation of a command in Tcl also produces a dictionary of return options. In general usage, all fIoption valuefR pairs given as arguments to fBreturnfR become entries in the return options dictionary, and any values at all are acceptable except as noted below. The fBcatchfR command may be used to capture all of this information (em the return code, the result, and the return options dictionary (em that arise from evaluation of a script.
As documented above, the fB-codefR entry in the return options dictionary receives special treatment by Tcl. There are other return options also recognized and treated specially by Tcl. They are:
-errorcode fIlistfR : The fB-errorcodefR option receives special treatment only when the value of the fB-codefR option is fBTCL_ERRORfR. Then the fIlistfR value is meant to be additional information about the error, presented as a Tcl list for further processing by programs. If no fB-errorcodefR option is provided to fBreturnfR when the fB-code errorfR option is provided, Tcl will set the value of the fB-errorcodefR entry in the return options dictionary to the default value of fBNONEfR. The fB-errorcodefR return option will also be stored in the global variable fBerrorCodefR.
-errorinfo fIinfofR : The fB-errorinfofR option receives special treatment only when the value of the fB-codefR option is fBTCL_ERRORfR. Then fIinfofR is the initial stack trace, meant to provide to a human reader additional information about the context in which the error occurred. The stack trace will also be stored in the global variable fBerrorInfofR. If no fB-errorinfofR option is provided to fBreturnfR when the fB-code errorfR option is provided, Tcl will provide its own initial stack trace value in the entry for fB-errorinfofR. Tcl's initial stack trace will include only the call to the procedure, and stack unwinding will append information about higher stack levels, but there will be no information about the context of the error within the procedure. Typically the fIinfofR value is supplied from the value of fB-errorinfofR in a return options dictionary captured by the fBcatchfR command (or from the copy of that information stored in the global variable fBerrorInfofR).
-errorstack fIlistfR : The fB-errorstackfR option receives special treatment only when the value of the fB-codefR option is fBTCL_ERRORfR. Then fIlistfR is the initial error stack, recording actual argument values passed to each proc level. The error stack will also be reachable through fBinfo errorstackfR. If no fB-errorstackfR option is provided to fBreturnfR when the fB-code errorfR option is provided, Tcl will provide its own initial error stack in the entry for fB-errorstackfR. Tcl's initial error stack will include only the call to the procedure, and stack unwinding will append information about higher stack levels, but there will be no information about the context of the error within the procedure. Typically the fIlistfR value is supplied from the value of fB-errorstackfR in a return options dictionary captured by the fBcatchfR command (or from the copy of that information from fBinfo errorstackfR).
-level fIlevelfR : The fB-levelfR and fB-codefR options work together to set the return code to be returned by one of the commands currently being evaluated. The fIlevelfR value must be a non-negative integer representing a number of levels on the call stack. It defines the number of levels up the stack at which the return code of a command currently being evaluated should be fIcodefR. If no fB-levelfR option is provided, the default value of fIlevelfR is 1, so that fBreturnfR sets the return code that the current procedure returns to its caller, 1 level up the call stack. The mechanism by which these options work is described in more detail below.
-options fIoptionsfR : The value fIoptionsfR must be a valid dictionary. The entries of that dictionary are treated as additional fIoption valuefR pairs for the fBreturnfR command.
Return code handling mechanisms
Return codes are used in Tcl to control program flow. A Tcl script is a sequence of Tcl commands. So long as each command evaluation returns a return code of fBTCL_OKfR, evaluation will continue to the next command in the script. Any exceptional return code (non-fBTCL_OKfR) returned by a command evaluation causes the flow on to the next command to be interrupted. Script evaluation ceases, and the exceptional return code from the command becomes the return code of the full script evaluation. This is the mechanism by which errors during script evaluation cause an interruption and unwinding of the call stack. It is also the mechanism by which commands like fBbreakfR, fBcontinuefR, and fBreturnfR cause script evaluation to terminate without evaluating all commands in sequence.
Some of Tcl's built-in commands evaluate scripts as part of their functioning. These commands can make use of exceptional return codes to enable special features. For example, the built-in Tcl commands that provide loops (em such as fBwhilefR, fBforfR, and fBforeachfR (em evaluate a script that is the body of the loop. If evaluation of the loop body returns the return code of fBTCL_BREAKfR or fBTCL_CONTINUEfR, the loop command can react in such a way as to give the fBbreakfR and fBcontinuefR commands their documented interpretation in loops.
Procedure invocation also involves evaluation of a script, the body of the procedure. Procedure invocation provides special treatment when evaluation of the procedure body returns the return code fBTCL_RETURNfR. In that circumstance, the fB-levelfR entry in the return options dictionary is decremented. If after decrementing, the value of the fB-levelfR entry is 0, then the value of the fB-codefR entry becomes the return code of the procedure. If after decrementing, the value of the fB-levelfR entry is greater than zero, then the return code of the procedure is fBTCL_RETURNfR. If the procedure invocation occurred during the evaluation of the body of another procedure, the process will repeat itself up the call stack, decrementing the value of the fB-levelfR entry at each level, so that the fIcodefR will be the return code of the current command fIlevelfR levels up the call stack. The fBsourcefR command performs the same handling of the fBTCL_RETURNfR return code, which explains the similarity of fBreturnfR invocation during a fBsourcefR to fBreturnfR invocation within a procedure.
The return code of the fBreturnfR command itself triggers this special handling by procedure invocation. If fBreturnfR is provided the option fB-level 0fR, then the return code of the fBreturnfR command itself will be the value fIcodefR of the fB-codefR option (or fBTCL_OKfR by default). Any other value for the fB-levelfR option (including the default value of 1) will cause the return code of the fBreturnfR command itself to be fBTCL_RETURNfR, triggering a return from the enclosing procedure.
Examples
First, a simple example of using fBreturnfR to return from a procedure, interrupting the procedure body.
proc printOneLine {} {
puts "line 1" ;# This line will be printed.
return
puts "line 2" ;# This line will not be printed.
}
Next, an example of using fBreturnfR to set the value returned by the procedure.
proc returnX {} {return X}
puts [returnX] ;# prints "X"
Next, a more complete example, using fBreturn -code errorfR to report invalid arguments.
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 factor [factorial [expr {$n - 1}]]
set product [expr {$n * $factor}]
return $product
}
Next, a procedure replacement for fBbreakfR.
proc myBreak {} {
return -code break
}
With the fB-level 0fR option, fBreturnfR itself can serve as a replacement for fBbreakfR, with the help of fBinterp aliasfR.
interp alias {} Break {} return -level 0 -code break
An example of using fBcatchfR and fBreturn -optionsfR to re-raise a caught error:
proc doSomething {} {
set resource [allocate]
catch {
# Long script of operations
# that might raise an error
} result options
deallocate $resource
return -options $options $result
}
Finally an example of advanced use of the fBreturnfR options to create a procedure replacement for fBreturnfR itself:
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
}