2011-04-22 94 views
3

爲什麼到廣場的工作第一個呼叫,但第二個沒有:PowerShell的功能參數

function add ($arg1,$arg2){ 
    return $arg1 + $arg2 
} 

function square ($arg1){ 
    $arg1 * $arg1 
} 

Write-Host (square (add 1 2)) 

Write-Host (square (add (1, 2))) 
+0

我剛纔看到逗號不用於參數傳遞在PowerShell中。 – Kyle 2011-04-22 15:03:29

回答

4

我想你看這個問題,但我會張貼所有的好更詳細的描述。

在您的第一行中,您對add的內部調用傳遞了兩個參數(整數)。該函數返回一個intsquare函數對其進行平方。

在第二行中,傳遞一個包含兩個整數的數組。這導致add函數將一個數組添加到空,並返回一個數組。然後square函數嘗試採用該數組並將其自行乘數。

你應該看起來像這樣的錯誤:

Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32". 
At line:1 char:8 
+ (1,2) * <<<< (1,2) 
    + CategoryInfo   : NotSpecified: (:) [], RuntimeException