Mr.Hien
Mr.Hien

Reputation: 85

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

I'm confused about parameter and argument in powershell. can you help me explain what is difference between param and arg ? Thanks.

Upvotes: 6

Views: 14020

Answers (2)

Keith Hill
Keith Hill

Reputation: 201662

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.

Upvotes: 3

manojlds
manojlds

Reputation: 301147

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

Upvotes: 10

Related Questions