Brackets at the end of a function

This topic contains 5 replies, has 6 voices, and was last updated by  Dan Potter 2 years, 6 months ago.

  • Author
  • #33888

    Graham Beer

    This is a little script from the brilliant Bruce Payette,

    Function tillXmas ()
        $now = [datetime]::Now
        [datetime] ( [string] $now.year + "-12-25") - $Now

    On the function, why has Bruce add () at the end of 'tillXmas' ?

  • #33889

    Dave Wyatt

    Habits of a .NET developer, I imagine. 🙂 It's just an empty parameter list, and it's optional. (If you use a param() block inside the function, such as for advanced functions, then you can't have the parentheses after the name; they're exclusive.)

  • #33902

    Graham Beer

    Thanks Dave. ☺

  • #33906

    Dan Potter

    It's used as an indicator for us to tell that the script was copied off the internet:-)

  • #33911

    Tim Curwick

    I am going to assume Bruce did it because it's faster. In my testing, you save an average 0.0008 milliseconds at runtime if you include the otherwise superfluous parentheses. : )

  • #33946

    Dan Potter

    Jokes aside.. I do a lot of forms based scripts and I regularly use the parens after function names where I'm not taking params from user input.

    Use param() inside the function if you need to set validation, switch, pipeline etc. settings.

You must be logged in to reply to this topic.