tcl中的proc,即C中的函数,proc可以调用另一个proc。proc的输入可以有参,也可以无参,有参的时候,可以设定参数的个数,也可以可变的参数个数,直接上代码。
# proc
# >>>
proc add {a b} {
return ($a + $b)
}
#调用proc example,使用默认的参数
#proc 就是c里面的封装函数
example
#使用可变函数
#example 1 View2
#example 2 View1
# >>> 如果arguments中最后一个是args,代表着arguments的数量是可变的,即可以大于3个,如以下代码展示
# >>> 在proc中return返回值即proc返回的值
proc example {first {second ""} args} {
if {$second eq ""} {
puts "there is only one argument and it is :$first"
return 1
} else {
if {$args eq ""} {
puts "there are two arguments. $first and $second"
return 2
} else {
puts "there are many arguments. $first and $second and $args"
return "many"
}
}
}
# >>> 将proc赋值给count1,也是用set
set count1 [example ONE]
set count2 [example ONE TWO]
set count3 [example ONE TWO THREE ]
set count4 [example ONE TWO THREE FOUR]
puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"
#>>> 结果
# %set count1 [example ONE]
# There is only one argument and it is: ONE
# 1
#% set count2 [example ONE TWO]
#There are two arguments - ONE and TWO
#2
#% set count3 [example ONE TWO THREE ]
#There are many arguments - ONE and TWO and THREE
#many
#% set count4 [example ONE TWO THREE FOUR]
#There are many arguments - ONE and TWO and THREE FOUR
#many
#%
#% puts "The example was called with $count1, $count2, $count3, and $count4 Arguments"
#The example was called with 1, 2, many, and many Arguments