powershell 在函数中调用 pow()

问题描述 投票:0回答:1

我希望我的

pow2($a,$n)
函数能够调用
[math]::pow($a,$n)
所以我写了这个 :

function pow2($a,$n) {
    return [math]::pow($a,$n)
}

但是当我调用它时,我收到此错误:

PS C:\> pow2(2,10)
Cannot convert argument "x", with value: "System.Object[]", for "Pow" to type "System.Double": "Cannot convert the "System.Object[]" value of
type "System.Object[]" to type "System.Double"."
At C:\Users\sebastien.mansfeld\OneDrive - PLURIAD – Groupe
Media-Participations\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:18 char:9
+     return [math]::pow($a,$n)
+            ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

PS C:\> 

我期待这个结果:

PS C:\> pow2(2,10)
1024
PS C:\> 
powershell function casting pow
1个回答
0
投票

你叫错了。

Powershell 中的函数参数调用时不带括号。

pow2 2 10
会起作用

你也不需要

return

© www.soinside.com 2019 - 2024. All rights reserved.