proc greet {} { return "Hello, Tcl!" } set message [greet] puts $message The greet procedure returns a string which is then stored in the message variable. The puts command displays it. Returning Multiple ValuesTcl procedures can return multiple values as a list using the return command. ...
return_values.tcl proc safeDivide {a b} { try { return [expr {$a / $b}] } trap {ARITH DIVZERO} {msg opts} { return "Error: division by zero" } } puts [safeDivide 10 2] puts [safeDivide 5 0] This function returns either the division result or an error message. The try ...
proc 生成一个新的命令,可以象固有 命令一样调用: % add 1 2 3 在定义过程时,你可以利用 return 命令在任何地方返回你想要的值. return 命令迅速中断过 程,并把它的参数作为过程的结果.例如: % proc abs {x} { if {$x >= 0} { return $x } return [expr -$x] } 过程的返回值是过程体中最后...
Default arguments are used to provide default values that can be used if no value is provided. An example for procedure with default arguments, which is sometimes referred as implicit arguments is shown below −Open Compiler #!/usr/bin/tclsh proc add {a {b 100} } { return [expr $a+$...
Arguments can have default values and are specified as follows: proc inc {value {increment 1}} { expr $value+$increment } incr 42 3 => 45 incr 42 => 43 Defaulted arguments, if any, must be the last arguments for the procedure. If a default is not specified, the argument is ...
Commands that return more than one object, such asget_cellsorget_sites, return a collection in the Vivado tool that looks and behaves like a native Tcl list. This feature allows performance gains when handling large lists of Tcl objects without the need to use special commands ...
puts $values;#输出black white 字典是否含有某个键 set colours [dict create colour3 black colour4 white] set result [dict exists $colours colour3] puts $result;#输出1 字典迭代 set colours [dict create colour3 black colour4 white] foreach key [dict keys $colours] { ...
The return command is optional in this example because the Tcl interpreter returns the value of the last command in the body as the value of the procedure. So, the procedure could be reduced to: proc Diag {a b} { expr sqrt($a * $a + $b * $b) } Note the stylized use of curly...
proc repeat {n body} { set res "" while {$n} { incr n -1 set res [uplevel $body] } return $res } Note that we take care to save the result of the last evaluation, so our repeat will (like most Tcl commands) return the last evaluated result. An example of usage: ...
proc 生成一个新的命令,可以象固有 命令一样调用: % add 1 2 3 在定义过程时,你可以利用 return 命令在任何地方返回你想要的值。 return 命令迅速中断过 程,并把它的参数作为过程的结果。例如: % proc abs {x} { if {$x = 0} { return $x } return [expr -$x] } 过程的返回值是过程体中最后...