Jump to content

Programming Fundamentals/Functions/Go

From Wikiversity

functions.go

[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://golang.org/doc/

package main

import "fmt"

func main() {
    var fahrenheit = GetFahrenheit()
    var celsius = CalculateCelsius(fahrenheit)
    DisplayResult(fahrenheit, celsius)
}

func GetFahrenheit() float32 {
    var fahrenheit float32
    
    fmt.Printf("Enter Fahrenheit temperature: ")
    fmt.Scanln(&fahrenheit)
    return fahrenheit
}

func CalculateCelsius(fahrenheit float32) float32 {
    var celsius = (fahrenheit - 32) * 5 / 9
    return celsius
}

func DisplayResult(fahrenheit float32, celsius float32) {
    fmt.Printf("%f° Fahrenheit is %f° Celsius\n", fahrenheit, celsius)
}

Try It

[edit | edit source]

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

See Also

[edit | edit source]