Programming Fundamentals/Functions/BASIC

functions.bas

edit
' 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/BASIC_Programming

DECLARE SUB Main
DECLARE FUNCTION GetFahrenheit
DECLARE FUNCTION CalculateCelsius(Fahrenheit)
DECLARE SUB DisplayResult(Fahrenheit, Celsius)

Main

SUB Main
    DIM Fahrenheit
    DIM Celsius

    Fahrenheit = GetFahrenheit
    Celsius = CalculateCelsius(Fahrenheit)
    DisplayResult Fahrenheit, Celsius
END SUB

FUNCTION GetFahrenheit()
    DIM Fahrenheit
    
    PRINT "Enter Fahrenheit temperature:"
    INPUT Fahrenheit
        
    GetFahrenheit = Fahrenheit
END FUNCTION

FUNCTION CalculateCelsius(Fahrenheit)
    DIM Celsius

    Celsius = (Fahrenheit - 32) * 5 / 9

    CalculateCelsius = Celsius
END FUNCTION

SUB DisplayResult(Fahrenheit, Celsius)
    PRINT STR$(Fahrenheit) + "° Fahrenheit is " + STR$(Celsius) + "° Celsius"
END SUB

Try It

edit

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

See Also

edit