How do I use modules correctly in a Fortran program?

Viewed 72

I built a speed, distance, and time calculator. I thought that it would be cool if you could go back to the main menu and calculate time after your original calculation (as an example). How would I do this by using modules? Here are the modules I have created:

module menu
  real :: s ! speed
  real :: d ! distance
  real :: t ! time
  real :: gg ! this is how I am going to switch between distance, time, and speed

  print *, 'Press 1 for speed, 2 for distance, and 3 for time'
  read*, gg
end menu

module speed
  print *, 'Input distance in metres'
  read *, d 
  print *, 'Input time in seconds'
  read *, t 
  s = d / t
  print *, 'Speed is ', s
end speed

module stay or leave
  print *, 'Press 4 to go back to menu, or press 5 to exit the console'
  read *, gg
end stay or leave

module distance
  print *, 'Input speed in metres per second'
  read *, s
  print *, 'Input time in seconds'
  read *, t 
  d = s * t
  print*, 'Distance is ', d
end distance

module time
  print *, 'Input distance in metres'
  read *, d
  print *, 'Input speed in metres per second'
  read *, s 
  t = d / s
  print*, 'Time is ', s 
end time
1 Answers

You are using module as a subroutine. A module is a collection of related subroutines, user types and other related data. There is no need to use modules in this example (at least not in the way it is shown above).

But if you had to use modules I have included an example below. The module definition contains the following subroutines

  • time_from_distance_and_speed()
  • distance_from_speed_and_time()
  • speed_from_time_and_distance()

and three common variables t, d, s used in the calculations. Although in general it not recommended re-using the same variables in different routines, this is done here for illustrative purposes to show how "global" variables can be defined in the module level.

Module

Here the module contains the variable definitions which are common to the procedures it contains. It also defines the three calculation processes.

module kinematics
implicit none
real :: t, d, s

contains

subroutine time_from_distance_and_speed()
print *, 'Input distance in metres'
read *, d
print *, 'Input speed in metres per second'
read *, s 
t = d / s
print*, 'Time is ', s 
end subroutine

subroutine distance_from_speed_and_time()
print *, 'Input speed in metres per second'
read *, s
print *, 'Input time in seconds'
read *, t 
d = s * t
print*, 'Distance is ', d
end subroutine

subroutine speed_from_time_and_distance()
print *, 'Input distance in metres'
read *, d 
print *, 'Input time in seconds'
read *, t 
s = d / t
print *, 'Speed is ', s
end subroutine

end module

Program

Here the main program uses the module defined above and calls the appropriate method depending on the user input.

program bike
use kinematics
integer :: gg
do while(.true.)
    print *, 'Press 1 for speed, 2 for distance, and 3 for time'
    read*, gg
    if(gg == 1) then
        call speed_from_time_and_distance
    else if(gg == 2) then
        call distance_from_speed_and_time
    else if(gg == 3) then
        call time_from_distance_and_speed
    end if
    print *, 'Press 5 to exit the console, anything else will repeat'
    read *, gg    
    if(gg== 5) then
        exit
    end if
end do

end program
Related