Jump to content

Programming Fundamentals/Functions/Fortran

From Wikiversity

functions.f90

[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.tutorialspoint.com/fortran
!   https://en.wikibooks.org/wiki/Fortran

program functions
    real :: get_fahrenheit, calculate_celsius
    real :: fahrenheit, celsius
    
    fahrenheit = get_fahrenheit()
    celsius = calculate_celsius(fahrenheit)
    call display_result(fahrenheit, celsius)
end program

function get_fahrenheit()
    real :: fahrenheit

    print ("(a)"), "Enter Fahrenheit temperature:"
    read *, fahrenheit
    get_fahrenheit = fahrenheit
end function

function calculate_celsius(fahrenheit)
    real :: fahrenheit, celsius

    celsius = (fahrenheit - 32) * 5 / 9
    calculate_celsius = celsius
end function

subroutine display_result(fahrenheit, celsius)
    real :: fahrenheit, celsius

    print "(f5.1 a f5.1 a)", fahrenheit, "° Fahrenheit is ", &
        celsius, "° Celsius"
end subroutine

Try It

[edit | edit source]

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

See Also

[edit | edit source]