34,333
edits
Changes
→Specifying Named Parameters
== Specifying Named Parameters ==
In the above example, we relied on the ''$args'' array to access any parameters which were passed through the function when it was called. Whilst this apprach works fine for simple functions, it can quickly become confusing as functions become more complex. An alternative mechanism is to declare the expected parameters and assign them variable names by which they may be accessed within the body of the function. This is achieved by placing the expected parameters in parentheses after the function name during the declaration. For example, the following function declares that two parameters are expectedwhen the function is called, and names them "$firstname" and "$lastname" respectively. <pre>function names ($firstname, $lastname) { "First name is $firstname" "Last name is $lastname"}</pre> With the parameter names specified, the may be passed through to the function in a number of different ways. The simplest way is to allow the function to allocate the parameters base don their position in the command line. For example: <pre>PS C:\Users\Administrator> names john smithFirst name is johnLast name is smith</pre> Alternatively, the parameters may be specified using the names, which allows them to be passed in any order. In the following example, the last name parameter is specified before the first name. Because the parameter names are specified, however, the values are still assigned to the correct parameters: <pre>PS C:\Users\Administrator> names -lastname smith -firstname johnFirst name is johnLast name is smith</pre> == Function Parameter Initialization == == Specifying Function Parameter Types == By default, if Windows PowerShell receives a parameter which is of the wrong type for the operations performing in the body of a function it will attempt to perform a type conversion to the correct type. For example, The following function multiplies two numbers passed through as arguments: <pre>function multiply ($val1, $val2) { "Performing calculation..." $val1 * $val2}</pre> If a string is passed through as a paramater, Windows PowerShell will attempt to convert it to a number. This may or may not be possible depending on the contents of the string. For example the following works: <pre>PS C:\Users\Administrator> multiply 2 "004"Performing calculation...8</pre> but the following does not: </pre>PS C:\Users\Administrator> multiply 2 "FOUR"Performing calculation...Cannot convert value "FOUR" to type "System.Int32". Error: "Input string was not in a correct format."At line:4 char:11+ $val1 * <<<< $val2</pre> Unfortunately, as can be seen from the above error message, the function was allowed to begin executing. It printed out the "Performing calculation..." message before the error was reported on line 3. Clearly it would be advantageous for the function to reject a parameter if it is not of a specified time. This can be achieved by using ''type casting'' in the parameter declaration. For example, to force the two parameters for our ''multiply'' function to be numbers they could be cast as type [int32]: <pre>function multiply ([int32] $val1, [int32] $val2) { "Performing calculation..." $val1 * $val2}</pre> Now when a parameter is specified that is both of the wrong type and unsuitable for conversion to the correct type, the function rejects the parameter immediately without commencing the execution process: <pre>PS C:\Users\Administrator> multiply 4 "five"multiply : Cannot convert value "five" to type "System.Int32". Error: "Input string was not in a correct format."At line:1 char:9+ multiply <<<< 4 "five"</pre>