<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Tcl 中的過程 > 原文: [https://zetcode.com/lang/tcl/procedures/](https://zetcode.com/lang/tcl/procedures/) 在本教程的這一部分中,我們將介紹 Tcl 過程。 過程是包含一系列命令的代碼塊。 在許多編程語言中,過程都稱為函數。 對于程序僅執行一個特定任務,這是一種良好的編程習慣。 程序為程序帶來了模塊化。 正確使用程序會帶來以下優點: * 減少代碼重復 * 將復雜的問題分解成更簡單的部分 * 提高代碼的清晰度 * 重用代碼 * 信息隱藏 過程有兩種基本類型:內置過程和用戶??定義的過程。 內置過程是 Tcl 核心語言的一部分。 例如,`rand()`,`sin()`和`exp()`是內置程序。 用戶定義的過程是使用`proc`關鍵字創建的過程。 `proc`關鍵字用于創建新的 Tcl 命令。 術語過程和命令通常可以互換使用。 我們從一個簡單的例子開始。 ```tcl #!/usr/bin/tclsh proc tclver {} { set v [info tclversion] puts "This is Tcl version $v" } tclver ``` 在此腳本中,我們創建一個簡單的`tclver`過程。 該過程將打印 Tcl 語言的版本。 ```tcl proc tclver {} { ``` 使用`proc`命令創建新過程。 `{}`字符表明該過程沒有參數。 ```tcl { set v [info tclversion] puts "This is Tcl version $v" } ``` 這是`tclver`過程的主體。 它在我們執行`tclver`命令時執行。 該命令的正文位于大括號之間。 ```tcl tclver ``` 通過指定其名稱來調用該過程。 ```tcl $ ./version.tcl This is Tcl version 8.6 ``` 樣本輸出。 ## 過程參數 參數是傳遞給過程的值。 過程可以采用一個或多個參數。 如果過程使用數據,則必須將數據傳遞給過程。 在下面的示例中,我們有一個采用一個參數的過程。 ```tcl #!/usr/bin/tclsh proc ftc {f} { return [expr $f * 9 / 5 + 32] } puts [ftc 100] puts [ftc 0] puts [ftc 30] ``` 我們創建一個`ftc`程序,將華氏溫度轉換為攝氏溫度。 ```tcl proc ftc {f} { ``` 該過程采用一個參數。 該程序的主體將使用其名稱`f`。 ```tcl return [expr $f * 9 / 5 + 32] ``` 我們計算攝氏溫度值。 `return`命令將值返回給調用方。 如果該過程未執行顯式返回,則其返回值是在過程主體中執行的最后一條命令的值。 ```tcl puts [ftc 100] ``` 執行`ftc`過程。 它以 100 為參數。 這是華氏溫度。 返回的值由`puts`命令使用,該命令將其打印到控制臺。 ```tcl $ ./fahrenheit.tcl 212 32 86 ``` 示例的輸出。 接下來,我們將有一個接受兩個參數的過程。 ```tcl #!/usr/bin/tclsh proc maximum {x y} { if {$x > $y} { return $x } else { return $y } } set a 23 set b 32 set val [maximum $a $b] puts "The max of $a, $b is $val" ``` `maximum`過程將兩個值的最大值相減。 ```tcl proc maximum {x y} { ``` 該方法有兩個參數。 ```tcl if {$x > $y} { return $x } else { return $y } ``` 在這里,我們計算哪個更大。 ```tcl set a 23 set b 32 ``` 我們定義了兩個要比較的變量。 ```tcl set val [maximum $a $b] ``` 我們計算兩個變量的最大值。 ```tcl $ ./maximum.tcl The max of 23, 32 is 32 ``` 這是`maximum.tcl`腳本的輸出。 ## 可變數量的參數 一個過程可以接受和處理可變數量的參數。 為此,我們使用特殊的`args`參數。 ```tcl #!/usr/bin/tclsh proc sum {args} { set s 0 foreach arg $args { incr s $arg } return $s } puts [sum 1 2 3 4] puts [sum 1 2] puts [sum 4] ``` 我們定義一個`sum`過程,將其所有參數加起來。 ```tcl proc sum {args} { ``` `sum`過程具有一個特殊的`args`參數。 它具有傳遞給該過程的所有值的列表。 ```tcl foreach arg $args { incr s $arg } ``` 我們遍歷列表并計算總和。 ```tcl puts [sum 1 2 3 4] puts [sum 1 2] puts [sum 4] ``` 我們調用`sum`過程 3 次。 在第一種情況下,它采用 4 個參數,在第二種情況下為 2,在最后一種情況下為 4。 ```tcl $ ./variable.tcl 10 3 4 ``` `variable.tcl`腳本的輸出。 ## 隱式參數 Tcl 過程中的參數可能具有隱式值。 如果未提供顯式值,則使用隱式值。 ```tcl #!/usr/bin/tclsh proc power {a {b 2}} { if {$b == 2} { return [expr $a * $a] } set value 1 for {set i 0} {$i<$b} {incr i} { set value [expr $value * $a] } return $value } set v1 [power 5] set v2 [power 5 4] puts "5^2 is $v1" puts "5^4 is $v2" ``` 在這里,我們創建一個`power`過程。 該過程具有一個帶有隱式值的參數。 我們可以使用一個和兩個參數來調用該過程。 ```tcl proc power {a {b 2}} { ``` 第二個參數`b`具有隱式值 2。如果僅提供一個參數,則`power`過程會將`a`的值返回到冪 2。 ```tcl set v1 [power 5] set v2 [power 5 4] ``` 我們用一個和兩個參數調用冪過程。 第一行計算 5 的冪 2。第二行計算 5 的冪 4。 ```tcl $ ./implicit.tcl 5^2 is 25 5^4 is 625 ``` 示例的輸出。 ## 返回多個值 `return`命令將一個值傳遞給調用方。 通常需要返回多個值。 在這種情況下,我們可以返回一個列表。 ```tcl #!/usr/bin/tclsh proc tworandoms {} { set r1 [expr round(rand()*10)] set r2 [expr round(rand()*10)] return [list $r1 $r2] } puts [tworandoms] puts [tworandoms] puts [tworandoms] puts [tworandoms] ``` 我們有一個`tworandoms`程序。 它返回 1 到 10 之間的兩個隨機整數。 ```tcl set r1 [expr round(rand()*10)] ``` 計算一個隨機整數并將其設置為`r1`變量。 ```tcl return [list $r1 $r2] ``` 借助`list`命令返回兩個值。 ```tcl $ ./tworandoms.tcl 3 7 1 3 8 7 9 9 ``` 樣本輸出。 ## 遞歸 在數學和計算機科學中,遞歸是一種定義函數的方法,其中所定義的函數在其自己的定義內應用。 換句話說,遞歸函數調用自身以完成其工作。 遞歸是解決許多編程任務的一種廣泛使用的方法。 遞歸是諸如 Scheme,OCalm 或 Clojure 之類的函數式語言的基本方法。 遞歸調用在 Tcl 中有一個限制。 遞歸調用不能超過 1000 個。 遞歸的典型示例是階乘的計算。 階乘`n!`是所有小于或等于 n 的正整數的乘積。 ```tcl #!/usr/bin/tclsh proc factorial n { if {$n==0} { return 1 } else { return [expr $n * [factorial [expr $n - 1]]] } } # Stack limit between 800 and 1000 levels puts [factorial 4] puts [factorial 10] puts [factorial 18] ``` 在此代碼示例中,我們計算三個數字的階乘。 ```tcl return [expr $n * [factorial [expr $n - 1]]] ``` 在`factorial`過程的主體內部,我們使用修改后的參數調用`factorial`過程。 該過程將自行調用。 ```tcl $ ./recursion.tcl 24 3628800 6402373705728000 ``` 這些就是結果。 如果我們嘗試計算 100 的階乘,則會收到“嵌套求值過多”的錯誤。 ## 作用域 在過程內部聲明的變量具有過程作用域。 名稱的作用域是程序文本的區域,在該區域內可以引用名稱聲明的實體而無需對該名稱進行限定。 在過程內部聲明的變量具有過程作用域; 它也稱為本地作用域。 該變量僅在此特定過程中有效。 ```tcl #!/usr/bin/tclsh proc test {} { puts "inside procedure" #puts "x is $x" set x 4 puts "x is $x" } set x 1 puts "outside procedure" puts "x is $x" test puts "outside procedure" puts "x is $x" ``` 在前面的示例中,我們在測試過程的內部和外部定義了一個`x`變量。 ```tcl set x 4 puts "x is $x" ``` 在測試過程中,我們定義了`x`變量。 該變量具有局部作用域,僅在此過程內有效。 ```tcl set x 1 puts "outside procedure" puts "x is $x" ``` 我們在過程外部定義`x`變量。 它具有全球作用域。 變量沒有沖突,因為它們具有不同的作用域。 ```tcl $ ./scope.tcl outside procedure x is 1 inside procedure x is 4 outside procedure x is 1 ``` 示例的輸出。 可以在過程內部更改全局變量。 ```tcl #!/usr/bin/tclsh proc test {} { upvar x y puts "inside procedure" puts "y is $y" set y 4 puts "y is $y" } set x 1 puts "outside procedure" puts "x is $x" test puts "outside procedure" puts "x is $x" ``` 我們定義一個全局`x`變量。 我們在測試過程中更改變量。 ```tcl upvar x y ``` 我們使用`upvar`命令通過名稱`y`引用全局`x`變量。 ```tcl set y 4 ``` 我們為本地`y`變量分配一個值,并更改全局`x`變量的值。 ```tcl $ ./scope2.tcl outside procedure x is 1 inside procedure y is 1 y is 4 outside procedure x is 4 ``` 從輸出中我們可以看到測試過程更改了`x`變量。 使用`global`命令,我們可以從過程中引用全局變量。 ```tcl #!/usr/bin/tclsh proc test {} { global x puts "inside test procedure x is $x" proc nested {} { global x puts "inside nested x is $x" } } set x 1 test nested puts "outside x is $x" ``` 在上面的示例中,我們有一個測試過程以及在該測試過程中定義的嵌套過程。 我們從這兩個過程中都引用了全局`x`變量。 ```tcl global x puts "inside test procedure x is $x" ``` 使用`global`命令,可以引用在測試過程之外定義的全局`x`變量。 ```tcl proc nested {} { global x puts "inside nested x is $x" } ``` 可以創建嵌套過程。 這些是在其他過程中定義的過程。 我們使用`global`命令引用全局`x`變量。 ```tcl test nested ``` 我們稱之為測試過程及其嵌套過程。 ```tcl $ ./scope3.tcl inside test procedure x is 1 inside nested x is 1 outside x is 1 ``` 示例的輸出。 在 Tcl 教程的這一部分中,我們介紹了 Tcl 過程。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看