Brackets at the end of a function

This topic contains 5 replies, has 6 voices, and was last updated by Profile photo of Dan Potter Dan Potter 11 months ago.

  • Author
    Posts
  • #33888
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Thanks Dave. ☺

  • #33906
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

  • #33911
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    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
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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.