Unfixable syntax error?

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Don Jones Don Jones 1 year, 10 months ago.

  • Author
    Posts
  • #22516
    Profile photo of Mason Emlen
    Mason Emlen
    Participant

    When I try to enter
    function Multiply (x, y) {
    return x * y
    }
    It comes back with the error (when I hover over the x) "Missing ')' function in parameter list".
    And when I hover over the ")" it says "Unexpected token ')' in expression or statement.
    Can someone please tell me what's going on?

  • #22518
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Variables in PowerShell begin with a $ character; you're missing that in both your parameter declaration and the function body.

    function Multiply ($x, $y) {
        return $x * $y
    }
    
  • #22519
    Profile photo of Don Jones
    Don Jones
    Keymaster

    And as a note, you're kind of approaching PowerShell with a traditional programming syntax. While it'll support that, to a degree, just understand that keywords like "return" aren't doing precisely the same thing you may be accustomed to in other languages. "Native" syntax in PowerShell would be...

    function multiply {
      param($x,$y)
      write-output ($x * $y)
    }
    

    Return and Write-Output aren't 100% exactly the same, but it's worth understanding the differences if you're going to dive into anything more complex.

You must be logged in to reply to this topic.