user18873951
user18873951

Reputation:

Where should USE statements be placed in Fortran?

I am making a physics calculator for my first-ever Fortran project. I already had the speed/distance/time part, which actually worked pretty well. However, when I tried to add the module for current, charge, and time, I ran into a problem- where do I put the USE statements for my modules? The code is below:

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
module electronics 
implicit none
real :: Q, I, T 

contains

subroutine charge_from_current_and_time()
print *, 'Input current in amps'
read *, I
print *, 'Input time in seconds'
read *, T 
Q = I * T
print*, 'Charge is ', Q 
end subroutine

subroutine current_from_charge_and_time()
print *, 'Input charge in coulombs'
read *, Q
print *, 'Input time in seconds'
read *, T 
C = Q/T
print*, 'Distance is ', d
end subroutine

subroutine time_from_current_and_charge()
print *, 'Input current in coulombs'
read *, Q 
print *, 'Input charge in amps'
read *, I 
T = Q/I
print *, 'Speed is ', s
end subroutine
end module

program bike
integer :: gg
integer :: pp

print *, 'Press 0 for speed, distance, and time. Press 2 for current, charge and time.'
read *, pp

if ( pp == 0 ) then
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, or press 4 to do another calculation'
    read *, gg    
    if(gg== 5) then
        exit
    end if
end do
end program

Upvotes: 0

Views: 377

Answers (1)

The use statements are put at the start of each compilation unit (module, program or procedure), right after the program, module, function or subroutine line and before any implicit statement or declarations. (You should have implicit none in each module and each program.)

For very short programs, if you leave out the program bike, you can start right with your use statements.

Upvotes: 1

Related Questions