tcl 中的过程:没有 return 语句,仍然得到返回值?
我是 tcl
的新手,想了解一下 return
语句在该语言中的工作原理。以下面的代码为例:
proc add name {
set name [expr $name+2]
}
set y 5
puts [add $y]
我惊讶地发现这实际上将值 7
打印到我的终端。由于我的程序中没有包含 return
语句,因此我预计不会打印出任何内容。
当我确实包含 return 语句时,它似乎会覆盖这个奇怪的默认行为:
proc add name {
set name [expr $name+2]
return hello
}
set y 5
puts [add $y]
上面的代码确实会返回 hello。但它对 7
有什么作用呢?
即使我没有 return 语句,为什么该过程仍然返回 7
?为什么当我提供 return 语句时它会覆盖此行为?
I'm new to tcl
, and would like to get some understanding of how return
statements work in the language. Take the following code, for instance:
proc add name {
set name [expr $name+2]
}
set y 5
puts [add $y]
I was surprised to find that this actually prints the value 7
to my terminal. I was expecting nothing to print out since I have not included a return
statement in my procedure.
When I do include a return statement, it seems to override this strange default behavior:
proc add name {
set name [expr $name+2]
return hello
}
set y 5
puts [add $y]
The above will indeed return hello. But what does it do with the 7
?
Why does the procedure still return a 7
even if I have no return statement? Why does it then override this behavior when I do provide a return statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您没有显式
返回
值,则将返回过程中最后执行的语句的值。这记录在 https://www.tcl-lang 中。 org/man/tcl8.6/TclCmd/proc.html 在描述部分的末尾。If you don't explicitly
return
a value, the value of the last statement executed in the proc will be returned. This is documented at https://www.tcl-lang.org/man/tcl8.6/TclCmd/proc.html at the end of the DESCRIPTION section.