# Functions in script

**URL:** <https://forums.powershell.org/t/functions-in-script/15531>\
**Category:** PowerShell Help\
**Created:** [January 26, 2021, 11:08pm UTC](https://forums.powershell.org/t/functions-in-script/15531 "2021-01-26T23:08:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maunilkshatriya](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/maunilkshatriya/32/15_2.png) [@maunilkshatriya](https://forums.powershell.org/u/maunilkshatriya)\
**Post date:** [January 26, 2021, 11:08pm UTC](https://forums.powershell.org/t/functions-in-script/15531/1 "2021-01-26T23:08:22Z")

</div>

Need to use two function in a single line. i.e. (Divide and multiplication should be in the same line of $result).

```auto
function Percentage([float]$a,[float]$b){
$result = divide $a $b
$result = multiply $result 100
return $result
}

```

---

<div class="post-metadata">

**Author:** ![andysvints](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/andysvints/32/1017_2.png) [@andysvints](https://forums.powershell.org/u/andysvints)\
**Post date:** [March 5, 2021, 9:19pm UTC](https://forums.powershell.org/t/functions-in-script/15531/2 "2021-03-05T21:19:36Z")

</div>

Hello @maunilkshatriya,  
You can use **$()** subexpression:

```auto
function Percentage([float]$a,[float]$b){

    return multiply $(divide $a $b) 100
}

```

Hope that helps.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 7, 2021, 2:58pm UTC](https://forums.powershell.org/t/functions-in-script/15531/3 "2021-03-07T14:58:58Z")

</div>

even without subexpressions too  
`multiply (divide $a $b) 100`

---

<div class="post-metadata">

**Author:** ![dicey](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dicey](https://forums.powershell.org/u/dicey)\
**Post date:** [March 8, 2021, 6:20am UTC](https://forums.powershell.org/t/functions-in-script/15531/4 "2021-03-08T06:20:49Z")

</div>

## Get-Percentage

- Returns the percentage of one float to another.
  - Two result types for duel functionality (display, math).
  - Output is `[string]` formatted as Percent when `-ShowSign` is declared.

- Adjust the scale (`5`) to control accuracy of result.
- PS Version: `5.1.14409.1018`

_Tip_: PowerShell 7.1 allows param attribute `[ValidateRange("Positive")]`. This could replace the need for a `try\catch` on the denominator value (not shown).

```powershell
function Get-Percentage([float]$num,[float]$denom,[switch]$ShowSign)
{
    switch ($ShowSign)
    {
        $true {[decimal]::Round($num/$denom,5).ToString("p"); break}
        $false {100*([decimal]::Round($num/$denom,5))}
    }
}

# test
[float]$a = 50; [float]$b = 100;
[float]$i = 7.654; [float]$j = 35.343;
cls;
Get-Percentage $a $b -ShowSign;
Get-Percentage $i $j -ShowSign;
Get-Percentage $i $j;

# results
50.00 %
21.66 %
21.65600

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:27pm UTC](https://forums.powershell.org/t/functions-in-script/15531/5 "2024-05-16T20:27:46Z")

</div>


