Why would a function have empty parentheses?

Welcome Forums General PowerShell Q&A Why would a function have empty parentheses?

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

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #112745

    Participant
    Points: 0
    Rank: Member

    I am going through some scripts and functions written be a predecessor, and quite a few functions start like this:

    Function SomethingCool()

    Why would you put empty parentheses in the function name like that?

    It seems to be in lieu of having a parameter set, and the function just assumes that the variables it has exist in the script that calls it?

     

    I have been unable to google my way to documentation about this, since the words "function" and "parentheses" (And various permutations) come up with unrelated results...

  • #112757

    Participant
    Points: 0
    Rank: Member

    Using Function SomethingCool() is a shorthand approach to something like:

    function Get-SomethingCool {
        param ()
        begin {}
        process {}
        end {}
    }
    

    My guess is the scripter learned in a diffent language that was more strict requiring a specific format and\or it nice to have consistency to your code. For instance, in my functions would all look like above, regardless if I have params or something in the begin block.

  • #112769

    Participant
    Points: 0
    Rank: Member

    Thanks, I suspected it was something like that, but couldn't find anything on the internets.

    My functions tend to look the same as yours 😉

  • #112772

    Participant
    Points: 0
    Rank: Member

    It's probably a coding pattern they picked up from elsewhere. For example, in C# a method (aka function) must always have parentheses to be valid syntax.

    It's not very good PowerShell syntax, but it is technically valid.

    • #112816

      Participant
      Points: 0
      Rank: Member

      Thanks, that is probably it. I know that this person had a bit of a coding background, so that would make sense.

You must be logged in to reply to this topic.