Brackets at the end of a function

Welcome Forums General PowerShell Q&A Brackets at the end of a function

This topic contains 5 replies, has 6 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #33888

    Participant
    Points: 0
    Rank: Member

    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

    Member
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    Thanks Dave. ☺

  • #33906

    Participant
    Points: 0
    Rank: Member

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

  • #33911

    Participant
    Points: 0
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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.

The topic ‘Brackets at the end of a function’ is closed to new replies.