Programming Fundamentals/Functions/VB.NET

From Wikiversity
Jump to navigation Jump to search

functions.vb[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.wikibooks.org/wiki/Visual_Basic_.NET

Public Module Main
    Sub Main
        Dim Fahrenheit As Double
        Dim Celsius As Double
        
        Fahrenheit = GetFahrenheit()
        Celsius = CalculateCelsius(Fahrenheit)
        DisplayResult(Fahrenheit, Celsius)
    End Sub

    Function GetFahrenheit() As Double
        Dim Input As String
        Dim Fahrenheit As Double
        
        Console.WriteLine("Enter Fahrenheit temperature:")
        Input = Console.ReadLine()
        Fahrenheit = Convert.ToDouble(Input)

        Return Fahrenheit
    End Function

    Function CalculateCelsius(Fahrenheit As Double) As Double
        Dim Celsius As Double
        
        Celsius = (Fahrenheit - 32) * 5 / 9
        
        Return Celsius
    End Function

    Sub DisplayResult(Fahrenheit As Double, Celsius As Double)
        Console.WriteLine(Fahrenheit.ToString() + "° Fahrenheit is " + Celsius.ToString() + "° Celsius")
    End Sub
End Module

Try It[edit | edit source]

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

See Also[edit | edit source]