The Parameters of a Function
The Parameters of a Function
Fundamentals of Parameters
Introduction
To perform its action(s), a function may need one or more values. If you are creating a function that will need values, you must indicate it. To do this, when creating a function, after its name, add some parentheses. Here is an example:
function CalculateTaxAmount() {
}
To indicate that the function will use an external value, in the parentheses of the function, type a name for that value. The name must be specified as if it were a variable. That is, start the name with a $ character. Here is an example:
function CalculateTaxAmount($abc) {
}
The name of the value specified in the parentheses of a function is called a parameter. In the body of the function, you can use the parameter or ignore it completely. This means that, in the body of the function, you can decide not to use a parameter. When using the parameter, you can involve it in any operation that is appropriate. For example, if it is a number, you can involve it in any algebraic operation.
Introduction to Calling a Function that Uses a Parameter
When calling a function that uses a parameter, you must provide a value for the parameter. To call a function that uses a parameter, type the name of the function. Then you have two options. After the name of the function, you can type a space and a value for the parameter. As another option, type the name of the function followed by parentheses. Inside the parameters, type the value of the parameter. The value provided for a parameter of a function is called an argument. The action of providing a value for the parameter of a function is referred to as passing an argument to the function.
Passing a Constant to a Function
There are various ways you can pass an argument to a function. If you have the value you want to use, we just saw that you can type the name of the function, a space, and a value for the parameter. Here is an example:
function EvaluateTaxLiability($paid) { $first3000 = 0 $next2000 = 0 $next5000 = 0 $over10000 = 0 if( ($paid -gt 0) -and ($paid -lt 3000) ) { $first3000 = 0.00 $next2000 = 0.00 $next5000 = 0.00 $over10000 = 0.00 } elseif( ($paid -gt 3000) -and ($paid -lt 5000) ) { $first3000 = 0.00 $next2000 = ($paid - 3000) * 3 / 100 $next5000 = 0.00 $over10000 = 0.00 } elseif( ($paid -gt 5000) -and ($paid -lt 10000) ) { $first3000 = 0.00 $next2000 = 2000 * 3 / 100 $next5000 = ($paid - 5000) * 4 / 100 $over10000 = 0.00 } else { # if(#paid -gt 10000) $first3000 = 0.00; $next2000 = 2000 * 3 / 100 $next5000 = 5000 * 4 / 100 $over10000 = ($paid - 10000) * 5 / 100 } $amt = $first3000 + $next2000 + $next5000 + $over10000 $netPay = $paid - $amt Write-Host "First $ 3000: $first3000" Write-Host "Next $ 2000: $next2000" Write-Host "Next $ 5000: $next5000" Write-Host "Over $ 10,000: $over10000" Write-Host '--------------------------------------------' Write-Host "Tax Amount: $amt" Write-Host '--------------------------------------------' Write-Host "Net Pay: $netPay" } Write-Host '============================================' Write-Host " - Amazing DeltaX - State Income Tax -" Write-Host "--------------------------------------------" Write-Host " -=- Mississippi -=-" Write-Host '============================================' Write-Host "Gross Salary: 12464.85" Write-Host '--------------------------------------------' EvaluateTaxLiability 12464.85 Write-Host '============================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 ============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- Mississippi -=- ============================================ Gross Salary: 12464.85 -------------------------------------------- First $ 3000: 0 Next $ 2000: 60 Next $ 5000: 200 Over $ 10,000: 123.2425 -------------------------------------------- Tax Amount: 383.2425 -------------------------------------------- Net Pay: 12081.6075 ============================================
Passing a Variable as Argument
In the above example, we provided a constant value for the argument. In some cases, the value you want to pass may come from a variable. In that case, you can provide the argument as a name of a variable. The name of the variable can be different from the name of the parameter. Here are examples:
function CalculateTaxAmount($grossSalary) { $taxAmount = 0 $netPay = 0 if( $grossSalary -lt 10000 ) { $taxAmount = $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { $taxAmount = 300.00 + (($grossSalary - 10000) * 4.00 / 100) } elseif (($grossSalary -ge 25000) -and ($grossSalary -lt 40000)) { $taxAmount = 900 + (($grossSalary - 25000) * 4.50 / 100) } elseif (($grossSalary -ge 40000) -and ($grossSalary -lt 60000)) { $taxAmount = 1575 + (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) $taxAmount = 2775 + (($grossSalary - 60000) * 6.50 / 100) } $netPay = $grossSalary - $taxAmount Write-Host "Tax Amount: $taxAmount" Write-Host "Net Pay: $netPay" } function CalculateExcess($grossSalary) { $excess = 0 if ($grossSalary -lt 10000) { $excess = $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { $excess = ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 25000) -and ($grossSalary -lt 40000) ) { $excess = ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 40000) -and ($grossSalary -lt 60000) ) { $excess = (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) $excess = ($grossSalary - 60000) * 6.50 / 100 } Write-Host "Excess: $excess" } $sal = 18755.50 Write-Host '======================================' Write-Host ' - Amazing DeltaX - State Income Tax -' Write-Host '--------------------------------------' Write-Host ' -=- West Virginia -=-' Write-Host '======================================' Write-Host "Gross Salary: $sal" Write-Host '--------------------------------------' # Passing an argument to a function without using parentheses CalculateExcess $sal # Passing an argument to a function using parentheses CalculateTaxAmount($sal) Write-Host '======================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 ====================================== - Amazing DeltaX - State Income Tax - -------------------------------------- -=- West Virginia -=- ====================================== Gross Salary: 18755.5 -------------------------------------- Excess: 350.22 Tax Amount: 650.22 Net Pay: 18105.28 ======================================
Calling a Function that Returns a Value
As you may know already, a function can return a vilue, and a function may use a parameter. When a function that uses a parameter also returns a value, you can call that function exactly as we have called the others so far. In some cases, you may want to use the value that the function returns. You have many options. As one option, you can assign the calling function to a variable, in which case the returned value of the function would be stored in the variable. Here are examples
function CalculateTaxAmount($grossSalary) { $taxAmount = 0.00 if( $grossSalary -lt 10000 ) { $taxAmount = $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { $taxAmount = 300.00 + (($grossSalary - 10000) * 4.00 / 100) } elseif (($grossSalary -ge 25000) -and ($grossSalary -lt 40000)) { $taxAmount = 900 + (($grossSalary - 25000) * 4.50 / 100) } elseif (($grossSalary -ge 40000) -and ($grossSalary -lt 60000)) { $taxAmount = 1575 + (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) $taxAmount = 2775 + (($grossSalary - 60000) * 6.50 / 100) } return $taxAmount } function CalculateExcess($grossSalary) { if ($grossSalary -lt 10000) { return $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { return ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 25000) -and ($grossSalary -lt 40000) ) { return ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 40000) -and ($grossSalary -lt 60000) ) { return (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) return ($grossSalary - 60000) * 6.50 / 100 } } $sal = 18755.50 $amt = CalculateTaxAmount $sal $netPay = $sal - $amt $excess = CalculateExcess($sal) Write-Host '======================================' Write-Host ' - Amazing DeltaX - State Income Tax -' Write-Host '--------------------------------------' Write-Host ' -=- West Virginia -=-' Write-Host '======================================' Write-Host "Gross Salary: $sal" Write-Host '--------------------------------------' Write-Host "Tax Amount: $amt" Write-Host "Excess: $excess" Write-Host "Net Pay: $netPay" Write-Host '======================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 ====================================== - Amazing DeltaX - State Income Tax - -------------------------------------- -=- West Virginia -=- ====================================== Gross Salary: 18755.5 -------------------------------------- Tax Amount: 650.22 Excess: 350.22 Net Pay: 18105.28 ======================================
In the above examples, we stored the value returned by the function in a variable. This may be necessary if you are planning to use that returned value more than once. If you are planning to use the value only once, you can call the function directly where its returned value is needed. One important rules is that you must put the function call in parentheses. Here is an example:
function CalculateTaxAmount($grossSalary) { $taxAmount = 0.00 if( $grossSalary -lt 10000 ) { $taxAmount = $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { $taxAmount = 300.00 + (($grossSalary - 10000) * 4.00 / 100) } elseif (($grossSalary -ge 25000) -and ($grossSalary -lt 40000)) { $taxAmount = 900 + (($grossSalary - 25000) * 4.50 / 100) } elseif (($grossSalary -ge 40000) -and ($grossSalary -lt 60000)) { $taxAmount = 1575 + (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) $taxAmount = 2775 + (($grossSalary - 60000) * 6.50 / 100) } return $taxAmount } function CalculateExcess($grossSalary) { if ($grossSalary -lt 10000) { return $grossSalary * 3.00 / 100 } elseif( ($grossSalary -ge 10000) -and ($grossSalary -lt 25000) ) { return ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 25000) -and ($grossSalary -lt 40000) ) { return ($grossSalary - 10000) * 4.00 / 100 } elseif( ($grossSalary -ge 40000) -and ($grossSalary -lt 60000) ) { return (($grossSalary - 40000) * 6.00 / 100) } else { # if ($grossSalary -ge 60000) return ($grossSalary - 60000) * 6.50 / 100 } } $sal = 26288.95 $amt = CalculateTaxAmount $sal $netPay = $sal - $amt Write-Host '======================================' Write-Host ' - Amazing DeltaX - State Income Tax -' Write-Host '--------------------------------------' Write-Host ' -=- West Virginia -=-' Write-Host '======================================' Write-Output "Gross Salary: $sal" Write-Output '--------------------------------------' Write-Output "Tax Amount: " (CalculateTaxAmount $sal) Write-Output "Excess: " (CalculateExcess($sal)) Write-Output "Net Pay: $netPay" Write-Host '======================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 ====================================== - Amazing DeltaX - State Income Tax - -------------------------------------- -=- West Virginia -=- ====================================== Gross Salary: 26288.95 -------------------------------------- Tax Amount: 958.00275 Excess: 651.558 Net Pay: 25330.94725 ======================================
A Function With Many Parameters
Introduction
A function can use many parameters, as many as you judge them necessary. When creating the function, in its parentheses, provide each parameter by a name. The parameters are separated by commas. Of course, the name of each parameter must start with $. Here is an example of a function that uses two parameters:
function show-time-worked($startTime, $endTime) {
}
Here is an example of a function that uses three parameters:
function show-time-worked($startTime, $timeWorked, $endTime) {
}
As mentioned earlier, you don't have to use a parameter in the body of the function if you don't have use for it. Otherwise, in the body of the function, you can use the parameters any appropriate way you want.
Calling a Function of Many Parameters
When calling a function that uses many parameters, you must provide a value for each parameter in the order the parameters appear in the parentheses.
![]() |
New Convention:From now on, in our lessons, we may write "The syntax of this function is" (or something to that effect): return-type function-name(parameter(s)); This means that we are referring to the function function-name that uses the parameter(s) specified. We will also provide (or reviewg) the return type of the function. In our new convention, we may terminate the syntax with a semi-colon. |
Here is an example of a function with two parameters and how to pass two arguments to it when calling it, using the option where parentheses are not used when calling the function:
function GetFullName($fn, $ln) { return $fn + " " + $ln } $firstName = "Michael" $lastName = "Carlock" $fullName = GetFullName $firstName, $lastName Write-Output '===================================' Write-Output "Employee Record" Write-Output "-----------------------------------" Write-Host "First Name: $firstName" Write-Host "Last Name: $lastName" Write-Host "Full Name: $fullName" Write-Output '==================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 =================================== Employee Record ----------------------------------- First Name: Michael Last Name: Carlock Full Name: Michael Carlock ===================================
Remember that you can also apply parentheses to the function when calling it. Here is an example:
function GetFullName($fn, $ln) { return $fn + " " + $ln } $firstName = "Michael" $lastName = "Carlock" $fullName = GetFullName($firstName, $lastName) Write-Output '===================================' Write-Output "Employee Record" Write-Output "-----------------------------------" Write-Host "First Name: $firstName" Write-Host "Last Name: $lastName" Write-Host "Full Name: $fullName" Write-Output '==================================='
Accessing a Parameter by Name
If you call a function that takes many arguments, you don't have to use the exact placeholder of each parameter. Instead, you can refer to each argument by the name of its parameter. To do that, when passing the arguments, don't add the parentheses to the name of the function. After typing the name of the function, type the name of the parameter (the name of the parameter where the function is defined, without $). For the name of the parameter, replace the $ symbol with -. After that name, leave a space, followed by the value you want for the parameter. You can do that for each argument. Here is an example:
function GetFullName($fn, $mn, $ln) {
return $fn + " " + $mn + " " + $ln
}
$fullName = GetFullName -mn "Bertrand" -ln "Yamaguchi" -fn "Paul"
Write-Output '==================================='
Write-Host "Employee Record"
Write-Host "-----------------------------------"
Write-Host "Full Name: $fullName"
Write-Output '==================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 =================================== Employee Record ----------------------------------- Full Name: Paul Bertrand Yamaguchi ===================================
As an option, or to make your code a little easy to read, when calling the function, you can add a colon (:) to the name of the parameter. Here are examples:
function GetFullName($fn, $mn, $ln) { return $fn + " " + $mn + " " + $ln } $fullName = GetFullName -ln: "Clinton" -fn: "William" -mn: "Jefferson" Write-Host '===================================' Write-Host "Employee Record" Write-Host "-----------------------------------" Write-Host "Full Name: $fullName" Write-Host '==================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 =================================== Employee Record ----------------------------------- Full Name: William Jefferson Clinton ===================================
A Function With Optional Values
A Parameter With an Optional Value
Consider the following code:
$originalPrice
function CalculatePriceAfterDiscount($rate) {
return $originalPrice - ($originalPrice * $rate / 100)
}
We have learned that if a function uses a parameter, when you call that function, you must provide a value for the argument. Here is an example:
$originalPrice function CalculatePriceAfterDiscount($rate) { return $originalPrice - ($originalPrice * $rate / 100) } $discountRate = 20.00 # 20% $originalPrice = 198.75 $priceAfterDiscount = CalculatePriceAfterDiscount($discountRate) Write-Host "Fun Department Store" Write-Host "===================================" Write-Output "Orignial Price: $originalPrice" Write-Output "Discount Rate: $discountRate%" Write-Host "-----------------------------------" Write-Output "Marked Price: $priceAfterDiscount" Write-Host "==================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 Fun Department Store =================================== Orignial Price: 198.75 Discount Rate: 20% ----------------------------------- Marked Price: 159 ===================================
If you have a function whose argument is usually given the same value, you can give a default value to that parameter. To specify that a parameter has a default value, when creating a function, in its parentheses, after the name of the parameter, assign a value to it. Here is an example:
$originalPrice
function CalculatePriceAfterDiscount($rate = 50) {
return $originalPrice - ($originalPrice * $rate / 100)
}
When calling the function, you can pass a value for the argument as we have dove so far. If you want to use the default value, omit passing the argument. If the function has only parameter and that parameter has a default value, when calling the function, don't add the parentheses to it. Here is an example:
$originalPrice
function CalculatePriceAfterDiscount($rate = 50) {
return $originalPrice - ($originalPrice * $rate / 100)
}
$discountRate = 20.00 # 20%
$originalPrice = 198.75
$priceAfterDiscount = CalculatePriceAfterDiscount
Write-Host "Fun Department Store"
Write-Host "==================================="
Write-Output "Orignial Price: $originalPrice"
Write-Output "Discount Rate: $discountRate%"
Write-Host "-----------------------------------"
Write-Output "Marked Price: $priceAfterDiscount"
Write-Host "==================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 Fun Department Store =================================== Orignial Price: 198.75 Discount Rate: 20% ----------------------------------- Marked Price: 99.375 ===================================
Otherwise, you can pass a value that is different from the default value of the argument. Here is an example:
$originalPrice
function CalculatePriceAfterDiscount($rate = 50) {
return $originalPrice - ($originalPrice * $rate / 100)
}
$discountRate = 20.00 # 20%
$originalPrice = 198.75
$priceAfterDiscount = CalculatePriceAfterDiscount(75)
Write-Host "Fun Department Store"
Write-Host "==================================="
Write-Output "Orignial Price: $originalPrice"
Write-Output "Discount Rate: $discountRate%"
Write-Host "-----------------------------------"
Write-Output "Marked Price: $priceAfterDiscount"
Write-Host "==================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 Fun Department Store =================================== Orignial Price: 198.75 Discount Rate: 20% ----------------------------------- Marked Price: 49.6875 ===================================
Parameters With Default Values
You can create a function that uses many parameters and some or all of those parameters can have default values. If a function uses more than one parameter, you can provide a default value for each and select which ones would have default values. If you want all parameters to have default values, when defining the function, type each name followed by = and followed by the desired value. When calling a function whose all parameters have default values, if you want to use all those default values, when calling the function, you can omit the arguments and use only the name of the function. Here is an example:
$originalPrice function CalculatePriceAfterDiscount($discountRate = 20) { return $originalPrice - ($originalPrice * $discountRate / 100) } function CalculateMarkedPrice($price = 200.00, $tax = 5.75, $discount = 50.00) { $markedPrice $afterDiscount = $price * $discount / 100.00 $taxValue = $afterDiscount * $tax / 100.00 $markedPrice = $afterDiscount + $taxValue return $markedPrice } $discountRate = 20.00 # 20% $originalPrice = 198.75 $priceAfterDiscount = CalculateMarkedPrice Write-Output "===================================" Write-Output "Fun Department Store" Write-Output "===================================" Write-Output "Orignial Price: $originalPrice" Write-Output "Discount Rate: $discountRate%" Write-Output "-----------------------------------" Write-Output "Marked Price: $priceAfterDiscount" Write-Output "==================================="
This would produce:
PS C:\Windows\System32> C:\Exercises\Parameters.ps1 =================================== Fun Department Store =================================== Orignial Price: 198.75 Discount Rate: 20% ----------------------------------- Marked Price: 105.75 ===================================
Rules on Parameters With Optional Values
If a function uses more than one parameter and you would like to provide default values for those parameters, the order of appearance of the arguments is important:
|
|||
Previous | Copyright © 2001-2025, FunctionX | Tuesday 11 February 2025, 16:57 | Next |
|