I have written a simple program in Fortran90 to calculate the area of a triangle. The user enters the three sides of the triangle, and then the program outputs the area. Simple enough.
MODULE Triangle_Operations
IMPLICIT NONE
CONTAINS
FUNCTION Area(x,y,z)
REAL :: Area ! function type
REAL, INTENT( IN ) :: x, y, z
REAL :: theta, height
theta = ACOS((x**2+y**2-z**2)/(2.0*x*y))
height = x*SIN(theta); Area = 0.5*y*height
END FUNCTION Area
END MODULE Triangle_Operations
PROGRAM Triangle
USE Triangle_Operations
IMPLICIT NONE
REAL :: a, b, c, Area
PRINT *, 'Welcome, please enter the &
&lengths of the 3 sides.'
READ *, a, b, c
PRINT *, 'Triangle''s area: ', Area(a,b,c)
END PROGRAM Triangle
When I compile this with gfortran
gfortran triangle1.f90
, this is the error I receive:
triangle1.f90:16.25:
REAL :: a, b, c, Area
1
triangle1.f90:14.8:
USE Triangle_Operations
2
Error: Symbol 'area' at (1) conflicts with symbol from module 'triangle_operations', use-associated at (2)
triangle1.f90:19.13:
READ *, a, b, c
1
Error: Symbol 'a' at (1) has no IMPLICIT type
triangle1.f90:19.16:
READ *, a, b, c
1
Error: Symbol 'b' at (1) has no IMPLICIT type
triangle1.f90:19.19:
READ *, a, b, c
1
Error: Symbol 'c' at (1) has no IMPLICIT type
Why exactly would there be an error thrown for variables a,b,c
? I have explicitly defined these as reals.
The problem is that you've defined
Area
twice -- once in your main program and once in the module you import and the names conflict. You likely think you need to defineArea
in the main program as a holdover from earlier (darker) times when calling a function without an explicit interface. In modern Fortran, modules provide interfaces automatically and the statementuse Triangle_operations
is enough.To fix your problem, remove the delcaration of
Area
from your main program, e.g. turninto
The subsequent errors in your compile output are a result of the first error concerning
Area
. That entire line gets invalidated so the type declarations ofa
,b
, andc
are not processed and this cause the compiler to complain about missing types the next time they are encountered. These errors will go away once you make the fix suggested above.If your intent had been to have a variable named
Area
in your main program to store the result of the module's function call, you could rename the module symbol, e.g.and then do this:
in your main program.