天天看点

TCL(Tool Command Language)学习笔记2-过程定义与使用

#过程定义及调用
proc sum {arg1 arg2} {
	return [expr $arg1+$arg2] 
}
puts "sum (2 3) \t=[sum (2 3) ]"
puts "sum \"2\" \"3\" \t=[sum "2" "3"]"
puts "sum 2 3 \t=[sum 2 3]"

#过程参数默认值与不定参数
proc proc_with_default_arg {arg1 {arg2 ""} args} {
	if {$arg2 == ""} {
		puts "There is only one argument and it is :$arg1"
		return 1
	} else {
		if {$args == ""} {
			puts "There are two arguments $arg1 and $arg2 "
			return 2
		} else {
			puts "There are many arguments :$arg1 ,$arg2 and $args"
			return "many"
		}
	}
}

set count1 [proc_with_default_arg ONE]
set count2 [proc_with_default_arg ONE TWO]
set count3 [proc_with_default_arg ONE TWO THREE]
set count4 [proc_with_default_arg ONE TWO THREE FOUR]

puts "The proc_with_default_arg was called with $count1,$count2,$count3,\
and $count4 Arguments."