2009-07-16

Access global variables in proc

在 procedure 裡存取全域變數,有二種做法:

1. 使用 global 命令宣告要存取的全域變數
2. 在 Tcl 支援 name space 之後,可以使用 name space 的表達方式(::)來存取變數
set x 0

proc appendMe {number} {
global x
incr x $number
}

proc appendMe2 {number} {
incr ::x $number
}

appendMe 3
puts $x

上面就是這二種方式的說明。

沒有留言: