Programming Fundamentals/Functions/PowerShell

From Wikiversity
Jump to navigation Jump to search

functions.ps1[edit | edit source]

# This program asks the user for a Fahrenheit temperature, 
# converts the given temperature to Celsius,
# and displays the results.
#
# References:
#     https://www.mathsisfun.com/temperature-conversion.html
#     https://en.wikiversity.org/wiki/PowerShell/Functions

function Get-Fahrenheit()
{
    Write-Host "Enter Fahrenheit temperature:"
    $fahrenheit = Read-Host
    return $fahrenheit
}

function Calculate-Celsius($fahrenheit)
{
    $celsius = ($fahrenheit - 32) * 5 / 9
    return $celsius
}

function Display-Result($fahrenheit, $celsius)
{
    Write-Host $($fahrenheit + '° Fahrenheit is ' + $celsius + '° Celsius')
}

function Main()
{
    $fahrenheit = Get-Fahrenheit
    $celsius = Calculate-Celsius $fahrenheit
    Display-Result $fahrenheit $celsius
}

Main

Try It[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own PowerShell compiler / interpreter / IDE.

Online[edit | edit source]

  • There are some options, but none are fully functional at this time.

Windows[edit | edit source]

macOS / Linux[edit | edit source]

See Also[edit | edit source]