Unfixable syntax error?

Welcome Forums General PowerShell Q&A Unfixable syntax error?

This topic contains 2 replies, has 3 voices, and was last updated by

 
Keymaster
3 years, 8 months ago.

  • Author
    Posts
  • #22516

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Keymaster
    Points: 7
    Rank: Member

    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.

The topic ‘Unfixable syntax error?’ is closed to new replies.