Shanti
Shanti

Reputation: 3

Fortran compiling error Two main programs

I've been trying to write this program for fortran where the pressure of a gas is calculated using the Van der Waals equation and everytime I've scrapped the entire code and started over because everything I do comes back with an error, even when I try using answers from other questions. I'm new to fortran and I know my syntax is probably all over, if someone could just please tell me where I'm going wrong it would be greatly appreciated.

    !Named program EquationOfState
 program EquationOfState
    implicit none
    real :: pressure
    write(*,*) 'pressure of Ideal Gas = '
    write(*,*) 'pressure calculated with Van der Waals equation = '

end program EquationOfState

!Prompt user to input values for density == d, and temperature == t
   write(*,*) "Enter density of gas"
   read(*,*) d
   write(*,*) "Enter temperature conditions"
   read(*,*) t
   write(*,*) "The pressure of the gas is", pressure


function pressure(d, t)
  real(kind=4) :: pressure_IdealGas
  real, intent(in) :: d, t
  real, parameter :: R = 286.9_4

pressure = d * R * t

end function pressure

with this error

 program EquationOfState
                        1
HWL_4.f90:14.36:

   write(*,*) "Enter density of gas"
                                    2
Error: Two main PROGRAMs at (1) and (2)

Upvotes: 0

Views: 1252

Answers (1)

Your lines:

!Prompt user to input values for density == d, and temperature == t
   write(*,*) "Enter density of gas"
   read(*,*) d
   write(*,*) "Enter temperature conditions"
   read(*,*) t
   write(*,*) "The pressure of the gas is", pressure

are outside of any program or function. You should at least move the end program statement behind these lines.


After you will do that you will realize there is no way to call your function from the main program as the variable pressure shadows the external function pressure(). You will have to rename one of them.


Also, your function is ill-formed, it is named pressure, but you declare something called pressure_IdealGas inside.

So you probably wanted:

function pressure_IdealGas(d, t)
  implicit none
  real :: pressure_IdealGas
  real, intent(in) :: d, t
  real, parameter :: R = 286.9_4

  pressure_IdealGas = d * R * t

end function pressure_IdealGas

and in the main program you wanted to call this function:

   pressure = pressure_IdealGas(d, t)
   write(*,*) "The pressure of the gas is", pressure

Notice the implicit none in the function. It is important to use it there, because the function is external and the implicit none in the program does not affect it.


The previous should make it work, but it is better not to use external functions and external subroutines. In very simple program, you can make the function internal by placing it between the keyword contains and end program but in serious programs you want to place it into a module:

module gas_functions
  implicit none
contains

  function pressure_IdealGas(d, t)
    real :: pressure_IdealGas
    real, intent(in) :: d, t
    real, parameter :: R = 286.9_4

    pressure_IdealGas = d * R * t

  end function pressure_IdealGas

end module

program EquationOfState
  use gas functions
  implicit none
  ...

Note that one implicit none per program is enough.

Read Correct use of modules, subroutines and functions in fortran for more.


Finally, there is no reason for the real(kind=4) in the function declaration. For compatibility, if you use just real everywhere, stick with it. The hardcoded number 4 is not a good idea for other reasons, see Fortran 90 kind parameter

Upvotes: 4

Related Questions