开发者

what's the difference between parameter and argument in powershell?

开发者 https://www.devze.com 2023-04-03 00:02 出处:网络
I\'m confused about parameter and argument in powershell. can you help me explain what is di开发者_JAVA技巧fference between param and arg ?

I'm confused about parameter and argument in powershell. can you help me explain what is di开发者_JAVA技巧fference between param and arg ? Thanks.


Are you talking about parameter defined with param and arguments accessed through $args?

In general, parameter is the variable which is part of the method's signature (method declaration). An argument is an expression used when calling the method.

But for the purpose of differentiating param and args, you can consider the former as defining parameters that can be either passed to the script (or function etc.) using the name of the parameter and supplying its value (named argument) or positional arguments specifying only the values and the latter as accessing positional arguments over and above the parameters expected by the script as defined in the param

Consider the following script named test.ps1:

param($param1,$param2)

write-host param1 is $param1 
write-host param2 is $param2

write-host arg1 is $args[0]
write-host arg2 is $args[1]

And suppose I call the script as:

.\test.ps1 1 2 3 4

I will get the output:

param1 is 1
param2 is 2
arg1 is 3
arg2 is 4

This is equivalent to calling it as:

.\test.ps1 -param1 1 -param2 2 3 4

or even

.\test.ps1 3 4 -param2 2 -param1 1


Traditionally in programming languages, parameter defines the inputs to a function where the function is declared. Arguments are the values supplied when calling the function. The argument values map to the function parameters. You can read more about this on Wikipedia.

0

精彩评论

暂无评论...
验证码 换一张
取 消