tcl指令碼學習十一:proc應用 (帶預設參數)

來源:互聯網
上載者:User

標籤:

lesson 11 : proc應用 (帶預設參數)


1.//使用過程的時候,不一定輸入所有的參數值。過程的輸入參數可以有預設值。預設值由
{預設參數名 預設值}指定。如果調用過程時沒有指定這些參數的值則會使用其預設值,
否則使用輸入值來替代預設值。在使用預設參數的時候要注意,如果預設參數之後還有
非預設參數,則在調用此過程的時候,預設參數的值也要求輸入,否則會出錯。這是因
為 Tcl 調用過程的時候是根據位置來匹配參數和輸入值的
2.如果參數列表中最後一個參數是 args 的話,則過程可以接收可變數目的輸入參數。當調用過
程時,除了指定參數以外的參數值都被 args 接收。如果參數列表中只有 args 一項,則 args 接
收所有輸入參數值

example ① :調用函數的傳回值的用法

proc Test { a {b 7} {str "Hello world"} } {
puts "$str"
return [expr $a * $b ]
}

puts "[Test 7 7 ] "


example ② :

proc test {args} {
puts "input values are : $args"
}

test my name is zhouli

result : input values are : my name is zhouli
//驗證了開頭第二點說的

 

example ③ :

proc config_sys {args} {
array set inArr $args
parray inArr
}

config_sys -sysName "HUB100" -ipAddr 192.168.10.1 -date 2003-11-21 -time 21:03:45


result :
inArr(-date) = 2003-11-21
inArr(-ipAddr) = 192.168.10.1
inArr(-sysName) = HUB100
inArr(-time) = 21:03:45

//先放一放


//進入本課正題!!咳咳

proc example {first {second ""} args} {
if {$second == ""} {
puts "There is only one argument and it is: $first";
return 1;
} else {
if {$args == ""} {
puts "There are two arguments - $first and $second";
return 2;
} else {
puts "There are many arguments - $first and $second and $args";
return "many";
}
}
}

set count1 [example ONE]
set count2 [example ONE TWO]
set count3 [example ONE TWO THREE ]
set count4 [example ONE TWO THREE FOUR]
//例子結合了很多簡單用法,不做解釋

 

tcl指令碼學習十一:proc應用 (帶預設參數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.