Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

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

like image 479
Mr.Hien Avatar asked Sep 06 '11 03:09

Mr.Hien


People also ask

What is the difference between parameter and argument?

Note the difference between parameters and arguments: Function parameters are the names listed in the function's definition. Function arguments are the real values passed to the function. Parameters are initialized to the values of the arguments supplied.

What is an argument in PowerShell?

Argument completion is a feature of PowerShell that provide hints, enables discovery, and speeds up input entry of argument values.

What is parameter and argument with example?

Parameters are components of functions Parameters identify values that are passed into a function. For example, a function to add three numbers might have three parameters. A function has a name, and it can be called from other points of a program. When that happens, the information passed is called an argument.


1 Answers

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
like image 83
manojlds Avatar answered Oct 13 '22 21:10

manojlds