What is the difference between a parameter and an argument in powershell?

I am confused by the parameter and argument in powershell. can you help me explain what is the difference between param and arg? Thanks.

+6
source share
2 answers

Traditionally, in programming languages, a parameter defines the inputs of the function in which the function is declared. Arguments are the values โ€‹โ€‹specified when the function was called. The values โ€‹โ€‹of the arguments are displayed in the function parameters. You can learn more about this on Wikipedia .

+3
source

Are you talking about a parameter defined with param , and arguments are available through $args ?

In general, a parameter is a variable that is part of a method signature (method declaration). An argument is an expression used when invoking a method.

But to differentiate param and args you can consider the former as defining parameters that can be passed to the script (or function, etc.) using the parameter name and its value (named argument) or positional arguments indicating only the values, and the latter - to access positional arguments over the parameters expected by the script, as defined in param

Consider the following script called 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 follows:

.\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 its name as:

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

or even

 .\test.ps1 3 4 -param2 2 -param1 1 
+9
source

Source: https://habr.com/ru/post/896678/


All Articles