I’m trying to write a simple function of the division, but I get an error
PS C:\Users\john> Function Div($x, $y) { $x / $y }
PS C:\Users\john> Div (1, 1)
Method invocation failed because [System.Object[]] doesn't contain a method named 'op_Division'.
At line:1 char:28
+ Function Div($x, $y) { $x / <<<< $y }
+ CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
What is my mistake? Thanks
You are invoking the function incorrectly. Powershell syntax for function invocation is:
Whereas (1,1) is an Object[].
If you want to prevent usage mistakes like this, declare the function as:
the [Parameter(Mandatory=$true)] ensures both values are given. And division always does double division in Powershell anyway, even if integers are given, so enforcing type [double] won’t stop integer usage and will make sure the input type is what you expect.