0

I want to build a function, but I get error saying, that I am not able to make a division. The funny thing is, that it is possible outside of the function.

function Resolution2AspectRatio($Width, $Height) {
    $tmp = $Width/$Height
    return $tmp
}

[System.Windows.Forms.Screen]::AllScreens | Select Bounds, Primary | ForEach-Object {

    $DisplayWidth = [int]$_.Bounds.Width
    $DisplayHeight = [int]$_.Bounds.Height
    Write-Host $($DisplayWidth/$DisplayHeight)
    Write-Host $(Resolution2AspectRatio($DisplayWidth, $DisplayHeight))
}

Any Idea how to solve this?

Thomas
  • 1,193
  • 1
  • 7
  • 16
  • In short: In PowerShell, functions are invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo(arg1, arg2)`; see [`Get-Help about_Parsing`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing). If you use `,` to separate arguments, you'll construct an _array_ that a function sees as a _single_ argument. To prevent accidental use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. – mklement0 Feb 07 '20 at 20:43
  • You do it like, Write-Host $(Resolution2AspectRatio $DisplayWidth $DisplayHeight) – Abdullah Leghari Feb 07 '20 at 20:43

1 Answers1

2

Whenever something works outside a function, but does not work inside the function, look at how you are calling the function and the type/value of the passed parameters.

In this case, part of the problem is the way you are calling the function in the Write-Host line. By putting the parameters inside parens and separating them with commas, you are telling PowerShell that those two values are a 2 element array of integers, and should be passed to the 1st parameter. Since you did not declare the type of your variables in the function, that was accepted, and your 2nd parameter remains empty.

I've added two Write-Host statements for you to see the values of your parameter variables after they are passed, and a "right" and "wrong" way of calling the function.

function Resolution2AspectRatio($Width, $Height) {
    Write-Host "Width = $Width"
    Write-Host "Height = $Height"
    $tmp = $Width / $Height
    return $tmp
}

[System.Windows.Forms.Screen]::AllScreens | Select Bounds, Primary | ForEach-Object {

    $DisplayWidth = [int]$_.Bounds.Width
    $DisplayHeight = [int]$_.Bounds.Height
    Write-Host $($DisplayWidth/$DisplayHeight)
    #Wrong way to call function
    Write-Host $(Resolution2AspectRatio($DisplayWidth,$DisplayHeight))
    #Right way to call function
    Write-Host $(Resolution2AspectRatio $DisplayWidth $DisplayHeight)
}
Matthew
  • 1,096
  • 7
  • 12
  • ugs.. thats looks ugly.... how can I change the function, that I am able to make calls the nice way with comma as param separator. – Thomas Feb 07 '20 at 20:43
  • @Thomas, the only way to make that work is to define your functions so that they only ever accept a _single, array-typed_ parameter, but I strongly advise against fighting the language this way. – mklement0 Feb 07 '20 at 20:48
  • thanks - I read now more about function params and examples https://stackoverflow.com/questions/4988226/how-do-i-pass-multiple-parameters-into-a-function-in-powershell – Thomas Feb 07 '20 at 20:52
  • 1
    Thanks for the pointer to the duplicate. I'm just learning the ropes here. :) – Matthew Feb 07 '20 at 21:09