Gfortran won't compile real variables

I wrote a simple Fortran90 program to calculate the area of ​​a triangle. The user enters the three sides of the triangle, and then the program draws out that 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' area: ', Area(a,b,c)
END PROGRAM Triangle

      

When I compile this with gfortran

gfortran triangle1.f90

, I get the following message:

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 was there an error for variables a,b,c

? I have clearly identified them as real.

+3


source to share


1 answer


The problem is that you have defined twice Area

- once in your main program and once in the module you are importing - and the name conflicts are in conflict. You probably feel like you need to define Area

in the main program as a delay from earlier (darker) times when calling a function without an explicit interface. In modern Fortran, modules provide interfaces automatically, and a statement is use Triangle_operations

sufficient.

To fix the problem, remove delcaration Area

from your main program eg. turn on

REAL :: a, b, c, Area

      

in

REAL :: a, b, c

      

Subsequent errors in your compilation output are the result of the first error associated with Area

. This whole line is invalidated, so the types of ads a

, b

and c

are not processed, and this leads to the fact that the compiler will complain about missing types in the next time they meet. These errors will go away once you do the fix suggested above.




If your intention was for your main program to have a named variable Area

to store the result of calling a module function, you can rename the module symbol, eg.

use triangle_operations, triangleArea => Area

      

and then do the following:

real a, b, c, Area
Area = triangleArea(a,b,c)

      

in your main program.

+1


source







All Articles