tcl [1]: proc 可变参数args

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

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值