Justin R.
Justin R.

Reputation: 24061

Powershell param declaration alternatives

What's the difference between:

function foo ([int] i, [string] s) { ... }

And:

function foo { 
    param (
        [int] i,
        [string] s
    )
    { ... }
}

The help on MSDN does not explain this. Thanks!

Upvotes: 4

Views: 951

Answers (3)

Shay Levy
Shay Levy

Reputation: 126842

Except for convenience, there's no difference. Both ways are valid. Using param (in my opinion) is more readable especially in advanced functions where a parameter declaration may contain a few lines of code and you can use indentation and line breaks.

Upvotes: 3

manojlds
manojlds

Reputation: 301327

No difference in this case. You might need to use the param declaration for advanced function parameters

Upvotes: 1

Mike Shepard
Mike Shepard

Reputation: 18166

Param statements are needed in scripts and scriptblocks, where there's not a natural place to define parameters like there would be in a function. Manojlds is correct that you have a lot of opportunities to use advanced function "options" to supercharge your function parameters if you use a param statement that you can't use in the more traditional parameter list.

Upvotes: 1

Related Questions