return.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:5k
源码类别:

通讯编程

开发平台:

Visual C++

  1. '"
  2. '" Copyright (c) 1993 The Regents of the University of California.
  3. '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
  4. '"
  5. '" See the file "license.terms" for information on usage and redistribution
  6. '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  7. '" 
  8. '" RCS: @(#) $Id: return.n,v 1.3.18.1 2004/10/27 14:23:58 dkf Exp $
  9. '" 
  10. .so man.macros
  11. .TH return n 7.0 Tcl "Tcl Built-In Commands"
  12. .BS
  13. '" Note:  do not modify the .SH NAME line immediately below!
  14. .SH NAME
  15. return - Return from a procedure
  16. .SH SYNOPSIS
  17. fBreturn fR?fB-code fIcodefR? ?fB-errorinfo fIinfofR? ?fB-errorcodefI codefR? ?fIstringfR?
  18. .BE
  19. .SH DESCRIPTION
  20. .PP
  21. Return immediately from the current procedure
  22. (or top-level command or fBsourcefR command),
  23. with fIstringfR as the return value.  If fIstringfR is not specified then
  24. an empty string will be returned as result.
  25. .SH "EXCEPTIONAL RETURN CODES"
  26. .PP
  27. In addition to the result of a procedure, the return
  28. code of a procedure may also be set by fBreturnfR
  29. through use of the fB-codefR option.
  30. In the usual case where the fB-codefR option isn't
  31. specified the procedure will return normally.
  32. However, the fB-codefR option may be used to generate an
  33. exceptional return from the procedure.
  34. fICodefR may have any of the following values:
  35. .TP 13
  36. fBok (or 0)fR
  37. Normal return:  same as if the option is omitted.  The return code
  38. of the procedure is 0 (fBTCL_OKfR).
  39. .TP 13
  40. fBerror (1)fR
  41. Error return: the return code of the procedure is 1 (fBTCL_ERRORfR).
  42. The procedure command behaves in its calling context as if it
  43. were the command fBerror fIresultfR.  See below for additional
  44. options.
  45. .TP 13
  46. fBreturn (2)fR
  47. The return code of the procedure is 2 (fBTCL_RETURNfR).  The
  48. procedure command behaves in its calling context as if it
  49. were the command fBreturnfR (with no arguments).
  50. .TP 13
  51. fBbreak (3)fR
  52. The return code of the procedure is 3 (fBTCL_BREAKfR).  The
  53. procedure command behaves in its calling context as if it
  54. were the command fBbreakfR.
  55. .TP 13
  56. fBcontinue (4)fR
  57. The return code of the procedure is 4 (fBTCL_CONTINUEfR).  The
  58. procedure command behaves in its calling context as if it
  59. were the command fBcontinuefR.
  60. .TP 13
  61. fIvaluefR
  62. fIValuefR must be an integer;  it will be returned as the
  63. return code for the current procedure.
  64. .LP
  65. The fB-codefR option is rarely used.
  66. It is provided so that procedures that implement
  67. new control structures can reflect exceptional conditions back to
  68. their callers.
  69. .PP
  70. Two additional options, fB-errorinfofR and fB-errorcodefR,
  71. may be used to provide additional information during error
  72. returns.
  73. These options are ignored unless fIcodefR is fBerrorfR.
  74. .PP
  75. The fB-errorinfofR option specifies an initial stack
  76. trace for the fBerrorInfofR variable;  if it is not specified then
  77. the stack trace left in fBerrorInfofR will include the call to
  78. the procedure and higher levels on the stack but it will not include
  79. any information about the context of the error within the procedure.
  80. Typically the fIinfofR value is supplied from the value left
  81. in fBerrorInfofR after a fBcatchfR command trapped an error within
  82. the procedure.
  83. .PP
  84. If the fB-errorcodefR option is specified then fIcodefR provides
  85. a value for the fBerrorCodefR variable.
  86. If the option is not specified then fBerrorCodefR will
  87. default to fBNONEfR.
  88. .SH EXAMPLES
  89. First, a simple example of using fBreturnfR to return from a
  90. procedure, interrupting the procedure body.
  91. .CS
  92. proc printOneLine {} {
  93.    puts "line 1"    ;# This line will be printed.
  94.    fBreturnfR
  95.    puts "line 2"    ;# This line will not be printed.
  96. }
  97. .CE
  98. .PP
  99. Next, an example of using fBreturnfR to set the value
  100. returned by the procedure.
  101. .CS
  102. proc returnX {} {fBreturnfR X}
  103. puts [returnX]    ;# prints "X"
  104. .CE
  105. .PP
  106. Next, a more complete example, using fBreturn -code errorfR
  107. to report invalid arguments.
  108. .CS
  109. proc factorial {n} {
  110.    if {![string is integer $n] || ($n < 0)} {
  111.       fBreturnfR -code error \
  112.             "expected non-negative integer,\
  113.              but got \"$n\""
  114.    }
  115.    if {$n < 2} {
  116.       fBreturnfR 1
  117.    }
  118.    set m [expr {$n - 1}]
  119.    set code [catch {factorial $m} factor]
  120.    if {$code != 0} {
  121.       fBreturnfR -code $code $factor
  122.    }
  123.    set product [expr {$n * $factor}]
  124.    if {$product < 0} {
  125.       fBreturnfR -code error \
  126.             "overflow computing factorial of $n"
  127.    }
  128.    fBreturnfR $product
  129. }
  130. .CE
  131. .PP
  132. Next, a procedure replacement for fBbreakfR.
  133. .CS
  134. proc myBreak {} {
  135.    fBreturnfR -code break
  136. }
  137. .CE
  138. .SH "SEE ALSO"
  139. break(n), catch(n), continue(n), error(n), proc(n), source(n), tclvars(n)
  140. .SH KEYWORDS
  141. break, catch, continue, error, procedure, return