SebMa
SebMa

Reputation: 4709

powershell calling pow() in function

I want my pow2($a,$n) function to call [math]::pow($a,$n) so I wrote this :

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

But when I call it I get this error :

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:\> 

I expect this result :

PS C:\> pow2(2,10)
1024
PS C:\> 

Upvotes: 0

Views: 77

Answers (1)

sirtao
sirtao

Reputation: 2720

You are Calling It Wrong.

Function Parameters in Powershell are called WITHOUT the parenthesis.

pow2 2 10 will work

Also you don't need return

Upvotes: 2

Related Questions